如何在此代码中提高GPS的准确度?
var map;
function initialize() {
var mapOptions = {
zoom: 18
};
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 marker = new google.maps.Marker({
position: pos,
map: map,
title: 'You Are Here'
});
map.setCenter(pos);
}, function() {
handleNoGeolocation(true);
});
} else {
// Browser doesn't support Geolocation
handleNoGeolocation(false);
}
}
有办法做到这一点吗?我知道EnableHighAccuracy是可能的:true,但我不知道我把这个命令放在哪里。有人有解决方案吗?
答案 0 :(得分:4)
引用表格Geolocation API spec:
interface Geolocation { void getCurrentPosition(PositionCallback successCallback, optional PositionErrorCallback errorCallback, optional PositionOptions options) [...] }
[...]
enableHighAccuracy属性提供了应用程序希望获得最佳结果的提示。
这意味着您可以在对象中将其作为最后一个参数传递(最小示例):
navigator.geolocation.getCurrentPosition(
function(position) { console.log(position); }, // Success callback
function() {}, // Error callback
{enableHighAccuracy: true} // Position Options
);