【发布时间】:2016-06-17 11:55:13
【问题描述】:
地理位置是谷歌。示例取自这里 https://developers.google.com/maps/documentation/javascript/examples/map-geolocation?hl=ru 。复制 JAVASCRIPT + HTML 并放置在本地主机上的虚拟主机 http://test.ru 上。创建浏览器密钥并添加它。地图已加载,但未在谷歌浏览器中运行地理定位。告诉我问题是什么。代码如下:
// 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.
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -34.397, lng: 150.644},
zoom: 6
});
var infoWindow = new google.maps.InfoWindow({map: map});
// 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.');
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.');
}`
【问题讨论】:
-
您在控制台上遇到任何错误?确保您在 chrome 浏览器上允许定位服务