Skip to content Skip to sidebar Skip to footer

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.

Solution 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 changesvarLatitude = position.coords.latitude;
    varLongitude = 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!

Post a Comment for "Using The W3c Geolocation Api's Watchposition Function"