Menelaos Bak..
27
您需要一个PositionOptions对象,在API中设置高精度标志.
getCurrentPosition()函数有一个可选的第三个参数,一个PositionOptions对象.您可以在PositionOptions对象中设置三个属性.所有属性都是可选的.您可以设置任何或全部或不设置它们.
POSITIONOPTIONS OBJECT
Property Type Default Notes
--------------------------------------------------------------
enableHighAccuracy Boolean false true might be slower
timeout long (no default) in milliseconds
maximumAge long 0 in milliseconds
所以,它应该像这样工作:
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
var accuracy = position.coords.accuracy;
var coords = new google.maps.LatLng(latitude, longitude);
var mapOptions = {
zoom: 15,
center: coords,
mapTypeControl: true,
navigationControlOptions: {
style: google.maps.NavigationControlStyle.SMALL
},
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var capa = document.getElementById("capa");
capa.innerHTML = "latitude: " + latitude + ", longitude: " + ", accuracy: " + accuracy;
map = new google.maps.Map(document.getElementById("mapContainer"), mapOptions);
var marker = new google.maps.Marker({
position: coords,
map: map,
title: "ok"
});
},
function error(msg) {alert('Please enable your GPS position feature.');},
{maximumAge:10000, timeout:5000, enableHighAccuracy: true});
} else {
alert("Geolocation API is not supported in your browser.");
}
注意到我添加了以下2个参数来getCurrentPosition调用:
function error(msg){alert('Please enable your GPS position future.');}
无法检索GPS或触发超时时调用此功能.
{maximumAge:10000, timeout:5000, enableHighAccuracy: true});
这些是选项.我们不希望gps数据超过10秒(maximumAge:10000).我们不希望等待超过5秒的响应(timeout:5000),我们希望启用高精度(enableHighAccuracy: true).
@meework您已将enableHighAccuracy设置为false,而不是将其设置为true. (3认同)