我无法弄清楚如何使用 W3C GeoLocation API 来查看用户的位置。最简单的方法是什么?我想要的只是更新用户的纬度和经度。
最佳答案
我刚刚在一个项目中使用了这个,我使用 Ben Nadel's blogpost 解决了这个问题以及 API 本身。
navigator.geolocation.watchPosition(function(position){
// These variables update every time the location changes
var Latitude = position.coords.latitude;
var Longitude = position.coords.longitude;
}, function(error){
// You can detect the reason and alert it; I chose not to.
alert('We could not get your location');
},{
// It'll stop looking after an hour. Enabling high accuracy tells it to use GPS if it's available
timeout: 5000,
maximumAge: 600000,
enableHighAccuracy: true
});
希望这有帮助!
关于javascript - 使用 W3C GeoLocation API watchPosition 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8248555/