如何在此代码中提高 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 是可能的,但我不知道我把这个命令放在哪里。有人有解决办法吗?
最佳答案
引用表格 Geolocation API spec :
interface Geolocation { void getCurrentPosition(PositionCallback successCallback, optional PositionErrorCallback errorCallback, optional PositionOptions options) [...] }
[...]
The enableHighAccuracy attribute provides a hint that the application would like to receive the best possible results.
这意味着您可以像这样将其作为最后一个参数传递给对象(最小示例):
navigator.geolocation.getCurrentPosition(
function(position) { console.log(position); }, // Success callback
function() {}, // Error callback
{enableHighAccuracy: true} // Position Options
);
关于javascript - 提高 GPS 的准确性(在 HTML5 地理定位中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30804460/