title: Geolocation description: Access GPS data.
This 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.
To get a few ideas, check out the sample at the bottom of this page or go straight to the reference content.
This API is based on the W3C Geolocation API Specification.
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.
This plugin defines a global navigator.geolocation object (for platforms
where it is otherwise missing).
Although the object is in the global scope, features provided by this plugin
are not available until after the deviceready event.
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
console.log("navigator.geolocation works well");
}
This requires cordova 5.0+ ( current stable 1.0.0 )
cordova plugin add cordova-plugin-geolocation
Older versions of cordova can still install via the deprecated id ( stale 0.3.12 )
cordova plugin add org.apache.cordova.geolocation
It is also possible to install via repo url directly ( unstable )
cordova plugin add https://github.com/apache/cordova-plugin-geolocation.git
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.
navigator.geolocation.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');
}
navigator.geolocation.getCurrentPosition(onSuccess, onError);
Since iOS 10 it's mandatory to provide an usage description in the info.plist if trying to access privacy-sensitive data. When the system prompts the user to allow access, this usage description string will displayed as part of the permission dialog box, but if you didn't provide the usage description, the app will crash before showing the dialog. Also, Apple will reject apps that access private data but don't provide an usage description.
This plugins requires the following usage description:
NSLocationWhenInUseUsageDescription describes the reason that the app accesses the user's location, this is used while the app is running in the foreground.NSLocationAlwaysAndWhenInUseUsageDescription describes the reason that the app is requesting access to the user’s location information at all times. Use this key if your iOS app accesses location information while running in the background and foreground. NSLocationAlwaysUsageDescription describes the reason that the app is requesting access to the user's location at all times. Use this key if your app accesses location information in the background and you deploy to a target earlier than iOS 11. For iOS 11 and later, add both NSLocationAlwaysUsageDescription and NSLocationAlwaysAndWhenInUseUsageDescription to your app’s Info.plist file with the same message.To add these entries into the info.plist, you can use the edit-config tag in the config.xml like this:
<edit-config target="NSLocationWhenInUseUsageDescription" file="*-Info.plist" mode="merge">
<string>need location access to find things nearby</string>
</edit-config>
<edit-config target="NSLocationAlwaysAndWhenInUseUsageDescription" file="*-Info.plist" mode="merge">
<string>need location access to find things nearby</string>
</edit-config>
<edit-config target="NSLocationAlwaysUsageDescription" file="*-Info.plist" mode="merge">
<string>need location access to find things nearby</string>
</edit-config>
For historic reasons, this plugin requires GPS Hardware on Android devices, so your app will not be able to run on devices without. If you want to use this plugin in your app, but you do not require actual GPS Hardware on the device, install the plugin using the variable GPS_REQUIRED set to false:
cordova plugin add cordova-plugin-geolocation --variable GPS_REQUIRED="false"
If Geolocation service is turned off the onError callback is invoked after timeout interval (if specified).
If timeout parameter is not specified then no callback is called.
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 = navigator.geolocation.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.
navigator.geolocation.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 + '
' +
'<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: 30000 });
Optional parameters to customize the retrieval of the geolocation
Position.
{ maximumAge: 3000, timeout: 5000, enableHighAccuracy: true };
enableHighAccuracy: Provides a hint that the application needs the best possible results. By default, the device attempts to retrieve a Position using network-based methods. Setting this property to true tells the framework to use more accurate methods, such as satellite positioning. (Boolean)
timeout: The maximum length of time (milliseconds) that is allowed to pass from the call to navigator.geolocation.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)
If Geolocation service is turned off the onError callback is invoked after timeout interval (if specified).
If timeout parameter is not specified then no callback is called.
Stop watching for changes to the device's location referenced by the
watchID parameter.
navigator.geolocation.clearWatch(watchID);
watchPosition interval to clear. (String)
// Options: watch for changes in position, and use the most
// accurate position acquisition method available.
//
var watchID = navigator.geolocation.watchPosition(onSuccess, onError, { enableHighAccuracy: true });
// ...later on...
navigator.geolocation.clearWatch(watchID);
Contains Position coordinates and timestamp, created by the geolocation API.
coords: A set of geographic coordinates. (Coordinates)
timestamp: Creation timestamp for coords. (DOMTimeStamp)
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
$ claude mcp add cordova-plugin-geolocation \
-- python -m otcore.mcp_server <graph>