user1062697
user1062697

Reputation: 1

Using the W3C GeoLocation API's watchPosition function

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.

Upvotes: 0

Views: 1361

Answers (1)

Julian
Julian

Reputation: 31

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!

Upvotes: 3

Related Questions