如何获取 Google Maps API 脚本?传感器参数设置为 true 还是 false ?
在我将“sensor=false”更改为“sensor=true”之前,Safari Geolocation 无法正常工作(在 Windows 下),如下例所示:
<script src="http://maps.googleapis.com/maps/api/js?sensor=true" type="text/javascript"></script>
它在所有浏览器中都能完美运行:IE9、Chrome、FF10+、Safari (Win)。
注意到另一个奇怪的事情 - 它只适用于 Safari 中的 WiFi - 如果你有线连接,它将无法工作,并且会一直尝试下去。
所以,要修复 Safari,只需添加以下 { maximumAge: 600000, timeout: 10000 } 来处理超时:
// Try W3C Geolocation (Preferred)
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
handleGeolocationAcquired(position.coords.latitude, position.coords.longitude);
}, function(error) {
handleNoGeolocation();
},
{ maximumAge: 600000, timeout: 10000 });
// Try Google Gears Geolocation
} else if (google.gears) {
var geo = google.gears.factory.create('beta.geolocation');
geo.getCurrentPosition(function (position) {
handleGeolocationAcquired(position.latitude, position.longitude);
}, function () {
handleNoGeolocation();
});
}
//Cannot obtain Geo Location
else {
handleNoGeolocation();
}
这样,如果您在 Safari(在 Windows 下)和有线连接(=> 无限循环获取地理位置):10 秒后,它将回退到错误处理程序。
BTW - maximumAge 参数只是设置位置过期时间。