我正在尝试在以下页面中使用 Google Maps API: https://developers.google.com/maps/documentation/javascript/geolocation
我所做的只是复制该页面上的代码并将其粘贴到我网站上的测试页面上。 http://kwt-events-com.stackstaging.com/content/api/maps.php
我有一个 API key 。我使用了它,页面加载谷歌地图时效果很好。
问题是它无法检测到显示此错误的位置: “错误:地理定位服务失败”
我尝试过的: 是允许在浏览器和计算机中进行位置检测。 我在同一台机器“macos”上尝试了不同的浏览器。 我在另一台机器上试过“另一台 pc 和另一台 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 之后安全提供页面的情况下,地理定位才可用。
关于php - 谷歌地图地理定位不请求许可,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52667666/