【发布时间】:2021-02-18 08:20:41
【问题描述】:
在我的网站中,我希望尽可能准确地获取访问者位置
我使用了 geolocation ,它正在工作并返回纬度和经度。 但问题是有时它们会以非常高的精度返回,有时它们不会 而且距离实际位置很远。
我已包含enableHighAccuracy: true 还需要什么?我该怎么做才能始终以高精度实现它?还是 99% 的时间?
这是输出示例
完全不准确
latitude: ****, longitude: ****, accuracy: 40852
非常准确
latitude: ****, longitude: ****, accuracy: 29
代码
function getCurrentLocation() {
console.log("test");
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) { //showLocation, ErrorHandler, options
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('lat');
capa.innerHTML = "latitude: " + latitude + ", longitude: " + 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.");
}
}
【问题讨论】:
-
我猜你依赖于浏览器返回的内容。您对此无能为力
-
但许多网站过去常常正确、完美地获取访问者的位置。我的意思是官方和关键网站,例如 gov
-
这取决于设备。准确度很大程度上取决于是否可以使用 GPS 芯片、手机信号塔数据、WiFi 位置等。在最坏的情况下,将使用 IP 地址,这将使您的准确性非常低。
标签: javascript html geolocation