Cordova Geolocation Plugin
Location-aware features — maps, "find nearby," delivery tracking, check-ins — all start with
the Geolocation plugin. Cordova's cordova-plugin-geolocation exposes the same
navigator.geolocation API defined by the W3C Geolocation standard used in regular
browsers, so if you've used geolocation on the web before, this will feel familiar. This page
covers a one-time position read, the shape of the data you get back, continuously watching
position changes, and the errors you need to handle.
Installing the Plugin
cordova plugin add cordova-plugin-geolocation
Once installed, navigator.geolocation is available globally in your app's
JavaScript after deviceready fires.
Getting a One-Time Position
For a single read of the device's current location — "where is the user right now" — use
navigator.geolocation.getCurrentPosition(successCallback, errorCallback, options).
It's asynchronous: the device may need a moment to acquire a GPS or network-based fix before
your success callback runs.
navigator.geolocation.getCurrentPosition(onSuccess, onError, {
enableHighAccuracy: true,
timeout: 10000,
maximumAge: 0
});
function onSuccess(position) {
console.log('Latitude: ' + position.coords.latitude);
console.log('Longitude: ' + position.coords.longitude);
}
function onError(error) {
console.log('Location error (' + error.code + '): ' + error.message);
}
The Position Object
The object passed to your success callback carries a nested coords object with
the actual location data:
| Property | What it holds |
|---|---|
| coords.latitude | Latitude in decimal degrees |
| coords.longitude | Longitude in decimal degrees |
| coords.accuracy | Estimated accuracy of the latitude/longitude reading, in meters — treat this as a radius of uncertainty, not an exact fix |
| coords.altitude | Altitude in meters above sea level, when available (may be null) |
| coords.speed | Current ground speed in meters/second, when available (may be null) |
| timestamp | The time the position was acquired |
Useful Options
| Option | What it controls |
|---|---|
| enableHighAccuracy | When true, asks the device to use GPS for a more precise (but slower and more battery-hungry) fix rather than falling back to Wi-Fi/cell-tower based positioning |
| timeout | Maximum time, in milliseconds, to wait for a position before calling the error callback with a timeout error |
| maximumAge | Accepts a cached position up to this many milliseconds old instead of requesting a brand-new fix — set to 0 to always force a fresh reading |
Watching Position Continuously
For features like live location tracking on a map, a single reading isn't enough — you need to
know whenever the device moves. navigator.geolocation.watchPosition() takes the
same arguments as getCurrentPosition(), but instead of firing once, it calls your
success callback repeatedly as the device's position changes. It returns a watch ID
that you must hold on to so you can stop watching later with
navigator.geolocation.clearWatch(watchId) — forgetting to clear a watch keeps the
GPS active and drains the battery even after your feature is no longer visible on screen.
var watchId = null;
function startWatch() {
watchId = navigator.geolocation.watchPosition(onPositionUpdate, onWatchError, {
enableHighAccuracy: true,
timeout: 15000,
maximumAge: 5000
});
}
function onPositionUpdate(position) {
console.log('Updated position: ' + position.coords.latitude + ', ' + position.coords.longitude);
}
function onWatchError(error) {
console.log('Watch error: ' + error.message);
}
function stopWatch() {
if (watchId !== null) {
navigator.geolocation.clearWatch(watchId);
watchId = null;
}
}
Wire startWatch() to a "Start Tracking" button and stopWatch() to a
"Stop Tracking" button, and you have a complete, safe start/stop location-watching feature that
never leaves the GPS running unnecessarily.
Handling Location Errors
The error object passed to your error callback has a numeric code property. The
Geolocation spec defines three possible values:
| Code | Constant | Meaning |
|---|---|---|
| 1 | PERMISSION_DENIED | The user (or the OS) refused to share location with your app |
| 2 | POSITION_UNAVAILABLE | The device couldn't determine a position — no GPS signal, no network-based location available |
| 3 | TIMEOUT | The timeout option elapsed before a position could be acquired |
PERMISSION_DENIED until they manually re-enable location permission for your app in system settings — your UI should explain why you need location before asking, and degrade gracefully if it's refused.