Using the W3C GeoLocation API's watchPosition function

空扰寡人 提交于 2019-12-13 05:05:50

问题


I cannot work out how to watch a user's location using the W3C GeoLocation API. What's the easiest way? All I want is an update of the user's latitude and longitude.


回答1:


I've just been using this in a project, I worked it out using Ben Nadel's blogpost and the API itself.

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!



来源:https://stackoverflow.com/questions/8248555/using-the-w3c-geolocation-apis-watchposition-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!