我正在使用 Google Maps API v3 Geolocation 来获取用户的实际位置。我从谷歌开发者那里找到了这篇文章:
https://developers.google.com/maps/documentation/javascript/examples/map-geolocation
这是我的代码(相当于谷歌地图示例的代码):
<head>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=true"></script>
<script>
// Note: This example requires that you consent to location sharing when
// prompted by your browser. If you see a blank space instead of the map, this
// is probably because you have denied permission for location sharing.
var map;
function initialize() {
var mapOptions = {
zoom: 6,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
// Try HTML5 geolocation
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = new google.maps.LatLng(position.coords.latitude,
position.coords.longitude);
var infowindow = new google.maps.InfoWindow({
map: map,
position: pos,
content: 'Location found using HTML5.'
});
map.setCenter(pos);
}, function() {
handleNoGeolocation(true);
});
} else {
// Browser doesn't support Geolocation
handleNoGeolocation(false);
}
}
function handleNoGeolocation(errorFlag) {
if (errorFlag) {
var content = 'Error: The Geolocation service failed.';
} else {
var content = 'Error: Your browser doesn\'t support geolocation.';
}
var options = {
map: map,
position: new google.maps.LatLng(60, 105),
content: content
};
var infowindow = new google.maps.InfoWindow(options);
map.setCenter(options.position);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="container">
<div class="entry-content">
<div class="blogtitle">IT</div><p> </p></td><td> </td><td><p> </p></td></tr></tbody></table><p> </p>
<div id="map-canvas"></div><!-- map-canvas end -->
</div><!-- entry-content end -->
</div><!-- container end -->
</body>
这在 Firefox 中工作得很好,但在 Google Chrome 和 Safari 中却不行。我认为因为 Chrome 和 Safari 是 webkit 浏览器。但我不知道如何解决它。
有人可以帮帮我吗?