louisbl / cordova-plugin-gpslocation

Native GPS Location for Cordova - Android
Apache License 2.0
20 stars 25 forks source link

Native GPS Location for Cordova - Android

This plugin provides information about the device's location, such as latitude and longitude. Source of location information is Global Positioning System (GPS). There is no guarantee that the API returns the device's actual location.

This API is based on the W3C Geolocation API Specification, and use the Android GPS provider.

WARNING: Collection and use of geolocation data raises important privacy issues. Your app's privacy policy should discuss how the app uses geolocation data, whether it is shared with any other parties, and the level of precision of the data (for example, coarse, fine, ZIP code level, etc.). Geolocation data is generally considered sensitive because it can reveal user's whereabouts and, if stored, the history of their travels. Therefore, in addition to the app's privacy policy, you should strongly consider providing a just-in-time notice before the app accesses geolocation data (if the device operating system doesn't do so already). That notice should provide the same information noted above, as well as obtaining the user's permission (e.g., by presenting choices for OK and No Thanks). For more information, please see the Privacy Guide.

Installation

The plugin is published on npm:

cordova plugin add cordova-plugin-gpslocation

If you wish to use the old Cordova registry, use the previous plugin id:

cordova plugin add fr.louisbl.cordova.gpslocation

Supported Platforms

Methods

Objects (Read-Only)

GPSLocation.getCurrentPosition

Returns the device's current position to the geolocationSuccess callback with a Position object as the parameter. If there is an error, the geolocationError callback is passed a PositionError object.

GPSLocation.getCurrentPosition(geolocationSuccess,
                                         [geolocationError],
                                         [geolocationOptions]);

Parameters

Example

// onSuccess Callback
// This method accepts a Position object, which contains the
// current GPS coordinates
//
var onSuccess = function(position) {
    alert('Latitude: '          + position.coords.latitude          + '\n' +
          'Longitude: '         + position.coords.longitude         + '\n' +
          'Altitude: '          + position.coords.altitude          + '\n' +
          'Accuracy: '          + position.coords.accuracy          + '\n' +
          'Altitude Accuracy: ' + position.coords.altitudeAccuracy  + '\n' +
          'Heading: '           + position.coords.heading           + '\n' +
          'Speed: '             + position.coords.speed             + '\n' +
          'Timestamp: '         + position.timestamp                + '\n');
};

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

GPSLocation.getCurrentPosition(onSuccess, onError);

GPSLocation.watchPosition

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.

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

Parameters

Returns

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 = GPSLocation.watchPosition(onSuccess, onError, { timeout: 30000 });

geolocationOptions

Optional parameters to customize the retrieval of the geolocation Position.

{ maximumAge: 3000, timeout: 5000 };

Options

GPSLocation.clearWatch

Stop watching for changes to the device's location referenced by the watchID parameter.

GPSLocation.clearWatch(watchID);

Parameters

Example

// Options: watch for changes in position, and use the most
// accurate position acquisition method available.
//
var watchID = GPSLocation.watchPosition(onSuccess, onError, { enableHighAccuracy: true });

// ...later on...

GPSLocation.clearWatch(watchID);

Position

Contains Position coordinates and timestamp, created by the geolocation API.

Properties

Coordinates

A Coordinates object is attached to a Position object that is available to callback functions in requests for the current position. It contains a set of properties that describe the geographic coordinates of a position.

Properties

Android Quirks

altitudeAccuracy: Not supported by Android devices, returning null.

PositionError

The PositionError object is passed to the geolocationError callback function when an error occurs with GPSLocation.

Properties

Constants