Android geolocation plugin using Google Play Services Location API.
npm install cordova-plugin-locationservices-fixThis plugin provides information about the device's location, such as
latitude and longitude. Common sources of location information include
Global Positioning System (GPS) and location inferred from network
signals such as IP address, RFID, WiFi and Bluetooth MAC addresses,
and GSM/CDMA cell IDs. 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 Google Play Services Location API.
__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.
The plugin is published on npm:
cordova plugin add cordova-plugin-locationservices
If you wish to use the old Cordova registry, use the previous plugin id:
cordova plugin add fr.louisbl.cordova.locationservices
Then add the Android Support and Google Repository.
- Android
- LocationServices.getCurrentPosition
- LocationServices.watchPosition
- LocationServices.clearWatch
- Position
- PositionError
- Coordinates
- Priorities
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 aPositionError object.
LocationServices.getCurrentPosition(geolocationSuccess,
[geolocationError],
[geolocationOptions]);
- __geolocationSuccess__: The callback that is passed the current position.
- __geolocationError__: _(Optional)_ The callback that executes if an error occurs.
- __geolocationOptions__: _(Optional)_ The geolocation options.
// 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');
}
LocationServices.getCurrentPosition(onSuccess, onError);
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 aPositionError object as the parameter.
var watchId = LocationServices.watchPosition(geolocationSuccess,
[geolocationError],
[geolocationOptions]);
- __geolocationSuccess__: The callback that is passed the current position.
- __geolocationError__: (Optional) The callback that executes if an error occurs.
- __geolocationOptions__: (Optional) The geolocation options.
- __String__: returns a watch id that references the watch position interval. The watch id should be used with LocationServices.clearWatch to stop watching for changes in position.
// 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 + '
' +
'Longitude: ' + position.coords.longitude + '
' +
'
// 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 = LocationServices.watchPosition(onSuccess, onError, { timeout: 30000, priority: LocationServices.priorities.PRIORITY_HIGH_ACCURACY });
Optional parameters to customize the retrieval of the geolocationPosition.
{ maximumAge: 3000, timeout: 5000, enableHighAccuracy: true, priority: LocationServices.priorities.PRIORITY_HIGH_ACCURACY, interval: 6000, fastInterval: 1000 };
- __enableHighAccuracy__: Provides a hint that the application needs the best possible results. It will force the plugin to check if the GPS is enabled before any action. _(Boolean)_
- __timeout__: The maximum length of time (milliseconds) that is allowed to pass from the call to LocationServices.getCurrentPosition or geolocation.watchPosition until the corresponding geolocationSuccess callback executes. If the geolocationSuccess callback is not invoked within this time, the geolocationError callback is passed a PositionError.TIMEOUT error code. (Note that when used in conjunction with geolocation.watchPosition, the geolocationError callback could be called on an interval every timeout milliseconds!) _(Number)_
- __maximumAge__: Accept a cached position whose age is no greater than the specified time in milliseconds. _(Number)_
- __priority__: The priority of the request is a strong hint for which location sources to use. For example, PRIORITY_HIGH_ACCURACY is more likely to use GPS, and PRIORITY_BALANCED_POWER_ACCURACY is more likely to use WIFI & Cell tower positioning, but it also depends on many other factors (such as which sources are available) and is implementation dependent. _(Number)_
- __interval__: Set the desired interval for active location updates, in milliseconds.
The location client will actively try to obtain location updates for your application at this interval, so it has a direct influence on the amount of power used by your application. Choose your interval wisely.
This interval is inexact. You may not receive updates at all (if no location sources are available), or you may receive them slower than requested. You may also receive them faster than requested (if other applications are requesting location at a faster interval). The fastest rate that that you will receive updates can be controlled with __fastInterval__. By default this fastest rate is 6x the interval frequency.
Applications with only the coarse location permission may have their interval silently throttled.
An interval of 0 is allowed, but not recommended, since location updates may be extremely fast on future implementations. _(Number)_
- __fastInterval__: Explicitly set the fastest interval for location updates, in milliseconds.
This controls the fastest rate at which your application will receive location updates, which might be faster than __interval__ in some situations (for example, if other applications are triggering location updates).
This allows your application to passively acquire locations at a rate faster than it actively acquires locations, saving power.
Unlike __interval__, this parameter is exact. Your application will never receive updates faster than this value.
If you don't call this method, a fastest interval will be selected for you. It will be a value faster than your active interval (__interval__).
An interval of 0 is allowed, but not recommended, since location updates may be extremely fast on future implementations. _(Number)_
Stop watching for changes to the device's location referenced by thewatchID parameter.
LocationServices.clearWatch(watchID);
- __watchID__: The id of the watchPosition interval to clear. (String)
// Options: watch for changes in position, and use the most
// accurate position acquisition method available.
//
var watchID = LocationServices.watchPosition(onSuccess, onError, { enableHighAccuracy: true });
// ...later on...
LocationServices.clearWatch(watchID);
Contains Position coordinates and timestamp, created by the geolocation API.
- __coords__: A set of geographic coordinates. _(Coordinates)_
- __timestamp__: Creation timestamp for coords. _(Date)_
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.
* __latitude__: Latitude in decimal degrees. _(Number)_
* __longitude__: Longitude in decimal degrees. _(Number)_
* __altitude__: Height of the position in meters above the ellipsoid. _(Number)_
* __accuracy__: Accuracy level of the latitude and longitude coordinates in meters. _(Number)_
* __altitudeAccuracy__: Accuracy level of the altitude coordinate in meters. _(Number)_
* __heading__: Direction of travel, specified in degrees counting clockwise relative to the true north. _(Number)_
* __speed__: Current ground speed of the device, specified in meters per second. _(Number)_
__altitudeAccuracy__: Not supported by Android devices, returning null.
The PositionError object is passed to the geolocationError
callback function when an error occurs with LocationServices.
- __code__: One of the predefined error codes listed below.
- __message__: Error message describing the details of the error encountered.
- PositionError.PERMISSION_DENIED
- Returned when users do not allow the app to retrieve position information. This is dependent on the platform.
- PositionError.POSITION_UNAVAILABLE
- Returned when the device is unable to retrieve a position. In general, this means the device is not connected to a network or can't get a satellite fix.
- PositionError.TIMEOUT
- Returned when the device is unable to retrieve a position within the time specified by the timeout included in geolocationOptions. When used with LocationServices.watchPosition, this error could be repeatedly passed to the geolocationError callback every timeout milliseconds.
This object holds the constants to use with __priority__ options.
- LocationServices.priorities.PRIORITY_HIGH_ACCURACY
- LocationServices.priorities.PRIORITY_BALANCED_POWER_ACCURACY
- LocationServices.priorities.PRIORITY_LOW_POWER
- LocationServices.priorities.PRIORITY_NO_POWER