Skip to content Skip to sidebar Skip to footer

How To Make A PhoneGap Application Save Current Geolocation Less Often?

I'm working on an app for fun which allows you to record routes you take. I'm using this to learn about the Google Maps API as well as Phonegap Build, so those are the tools I'm us

Solution 1:

you should use navigator.geolocation.watchPosition It returns the device's current position when a change in position is detected. When the device retrieves a new location, the geolocationSuccess callback executes with a Position object as the parameter. If there is an error, the geolocationError callback executes with a PositionError object as the parameter.

syntax

var watchId = navigator.geolocation.watchPosition(geolocationSuccess,
                                              [geolocationError],
                                              [geolocationOptions]);

Example

// onSuccess Callback
//   This method accepts a `Position` object, which contains
//   the current GPS coordinates
//
function onSuccess(position) {
    var element = document.getElementById('geolocation');
    element.innerHTML = 'Latitude: '  + position.coords.latitude      + '<br />' +
                        'Longitude: ' + position.coords.longitude     + '<br />' +
                        '<hr />'      + element.innerHTML;
}

// onError Callback receives a PositionError object
//
function onError(error) {
    alert('code: '    + error.code    + '\n' +
          'message: ' + error.message + '\n');
}

// Options: throw an error if no update is received every 30 seconds.
//
var watchID = navigator.geolocation.watchPosition(onSuccess, onError, { timeout: 3000 });

now, this way location will be captured only if device is moved so route with less plot points. change timeout as per your requirement.

clearwatch when you want to stop watching for changes to the device's location.


Post a Comment for "How To Make A PhoneGap Application Save Current Geolocation Less Often?"