简体   繁体   English

使用W3C GeoLocation API的watchPosition函数

[英]Using the W3C GeoLocation API's watchPosition function

I cannot work out how to watch a user's location using the W3C GeoLocation API. 我无法弄清楚如何使用W3C GeoLocation API监视用户的位置。 What's the easiest way? 最简单的方法是什么? All I want is an update of the user's latitude and longitude. 我想要的只是更新用户的纬度和经度。

I've just been using this in a project, I worked it out using Ben Nadel's blogpost and the API itself. 我刚刚在一个项目中使用了它,我使用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
});

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM