php - 谷歌地图地理定位不征求许可
问题描述
我正在尝试在以下页面中使用 Google Maps API: https ://developers.google.com/maps/documentation/javascript/geolocation
我所做的只是复制该页面上的代码并将其粘贴到我网站上的测试页面上。 http://kwt-events-com.stackstaging.com/content/api/maps.php
我确实有一个 API 密钥。我用过它,页面加载得很好,谷歌地图。
问题是它无法检测到显示此错误的位置:“错误:地理位置服务失败”
我尝试过 的是:允许在浏览器和计算机中进行位置检测。我在同一台机器“macos”上尝试了不同的浏览器。我在另一台机器上试过“另一台电脑和另一台 Mac”。我在手机“ios”上试过。
相同的错误
我知道我不能发布所有代码,但我会这样做,所以你们可以帮助我
<!DOCTYPE html>
<html>
<head>
<title>Geolocation</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html, body {
height: 100%;
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
// Note: This example requires that you consent to location sharing when
// prompted by your browser. If you see the error "The Geolocation service
// failed.", it means you probably did not give permission for the browser to
// locate you.
var map, infoWindow;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -34.397, lng: 150.644},
zoom: 6
});
infoWindow = new google.maps.InfoWindow;
// Try HTML5 geolocation.
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
infoWindow.setPosition(pos);
infoWindow.setContent('Location found.');
infoWindow.open(map);
map.setCenter(pos);
}, function() {
handleLocationError(true, infoWindow, map.getCenter());
});
} else {
// Browser doesn't support Geolocation
handleLocationError(false, infoWindow, map.getCenter());
}
}
function handleLocationError(browserHasGeolocation, infoWindow, pos) {
infoWindow.setPosition(pos);
infoWindow.setContent(browserHasGeolocation ?
'Error: The Geolocation service failed.' :
'Error: Your browser doesn\'t support geolocation.');
infoWindow.open(map);
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=IHAD-MY-API-KEY-HERE&callback=initMap">
</script>
解决方案
您的页面是通过 HTTP 而不是 HTTPS 提供的,并且仅当页面自 Chrome 50、Safari 10 和 Firefox 55 起安全提供时,地理定位才可用。
推荐阅读
- r - R包安装失败:ld:找不到框架CoreFoundation clang-7:错误:链接器命令失败,退出代码为1
- python - 如何在打印结果中没有元素本身的情况下执行循环
- javascript - 在JS中取出Object的子属性
- python - 如何使动态对象名称分配给一个类?/ 如何创建多个对象名称不同的窗口 Tkinter
- django - vimeo 从 django 上传私人视频
- sql-server - 如何解决 Azure CD 中的 SQL 数据库数据移动错误
- excel - Application.Onkey 的问题
- python - python中的偏斜t分布
- r - 为什么 `pivot_wider` 不能在 `data.table` 上工作
- azure - 具有存储服务终结点策略的 VNET 中的 Azure Batch