DedrisFrameworkDedrisFramework

← Plugins  /  Geolocation

📍

Geolocation

cordova-plugin-geolocation
iOS Android 🌐 Browser

This official Apache Cordova plugin reads the device's current GPS position and lets you subscribe to location changes as the user moves. Its interface mirrors the W3C Geolocation API, so the methods and the data they return will feel familiar if you have used geolocation in the browser.

Installation

Add the plugin to your Cordova project from the command line:

terminal
cordova plugin add cordova-plugin-geolocation

Methods

The plugin attaches its methods to navigator.geolocation. Call them after the deviceready event has fired:

MethodDescription
navigator.geolocation.getCurrentPosition(success, error, options)Fetches a single, one-shot reading of the device's current position.
navigator.geolocation.watchPosition(success, error, options)Returns a watch id and keeps calling success each time the device moves.
navigator.geolocation.clearWatch(watchId)Stops the active watch identified by watchId.

The position object

The success callback receives a position object whose location data lives under coords:

PropertyTypeDescription
coords.latitudeNumberLatitude in decimal degrees.
coords.longitudeNumberLongitude in decimal degrees.
coords.altitudeNumberHeight above sea level in metres.
coords.accuracyNumberAccuracy of the latitude and longitude, in metres.
coords.altitudeAccuracyNumberAccuracy of the altitude reading, in metres.
coords.headingNumberDirection of travel in degrees, measured clockwise from north.
coords.speedNumberCurrent ground speed in metres per second.
timestampNumberWhen the reading was taken, as a millisecond timestamp.

Options

Pass an options object to tune how the position is read:

PropertyTypeDescription
enableHighAccuracyBooleanRequest the most precise fix the device can provide.
timeoutNumberHow long, in milliseconds, to wait for a reading before failing.
maximumAgeNumberAccept a cached fix up to this many milliseconds old.

Usage example

Read the device's current location once and log the coordinates:

www/js/app.js
navigator.geolocation.getCurrentPosition(onSuccess, onError, {
  enableHighAccuracy: true
});

function onSuccess(position) {
  console.log("Lat: " + position.coords.latitude);
  console.log("Lng: " + position.coords.longitude);
}

function onError(error) {
  console.log("Code: " + error.code);
  console.log("Message: " + error.message);
}

Tips

📚 Source: the technical reference on this page (the methods, the position object and the install command) is based on the official Apache Cordova geolocation documentation. Descriptions and examples have been rewritten in DedrisFramework's own words. Apache Cordova documentation is published by the Apache Software Foundation under the Apache License 2.0.
← Back to plugins Start a project