javascript - 使用 W3C GeoLocation API watchPosition 函数

标签 javascript geolocation

我无法弄清楚如何使用 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/

相关文章:

javascript - phonegap 安卓相机 : crash when "correctOrientation" option set

javascript - .off ("DOMSubtreeModified"的问题)

javascript - 无法使用 AngularJs 获取文本区域值

binding - 具有 Angular2 数据绑定(bind)的 NativeScript 不适用于地理定位

javascript - 如何修复 "EJS unable to access DOM causing Document is not Defined "

javascript - 无法在 Ionic Framework 上使用 Crosswalk 获取地理定位(未发现错误)

javascript - JS : Objects into array

javascript - 自定义选择选项作为 mysql 填充下拉列表中的第一个选项

ios - Cordova geolocation.getCurrentPosition 在 iPad 上抛出超时

google-maps-api-3 - 谷歌地图 API watchPosition