使用W3C GeoLocation API'的watchPosition函数

Using the W3C GeoLocation API's watchPosition function

本文关键字:watchPosition 函数 W3C GeoLocation API 使用      更新时间:2023-09-26

我不知道如何使用W3C GeoLocation API来观察用户的位置。最简单的方法是什么?我想要的只是更新用户的纬度和经度。

我刚刚在一个项目中使用了这个,我通过Ben Nadel的博客文章和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
});

希望这对你有帮助!