Geolocation Plugin
Tested Version: 4.0.2
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.
This API is based on the W3C Geolocation API Specification.
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");
}Plugin ID
cordova-plugin-geolocationAdding the Plugin in Monaca
In order to use this plugin, please enable Geolocation plugin in Monaca Cloud IDE.
Supported Platforms
Android
iOS
API Reference
Methods
navigator.geolocation.getCurrentPosition
navigator.geolocation.watchPosition
navigator.geolocation.clearWatch
navigator.geolocation.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.
Parameters
geolocationSuccess: The callback that is passed the current position.
geolocationError: (Optional) The callback that executes if an error occurs.
geolocationOptions: (Optional) The geolocation options.
Example
iOS Quirks
Since iOS 10, it's mandatory to provide 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 usage description.
This plugins requires the following usage description:
NSLocationWhenInUseUsageDescriptiondescribes the reason that the app accesses the user's location.
To add this entry into the info.plist, you can use the edit-config tag in the config.xml like this:
Android Quirks
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.
navigator.geolocation.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.
Parameters
geolocationSuccess: The callback that is passed the current position.
geolocationError: (Optional) The callback that executes if an error occurs.
geolocationOptions: (Optional) The geolocation options.
Returns
String: returns a watch id that references the watch position interval. The watch id should be used with
navigator.geolocation.clearWatchto stop watching for changes in position.
Example
geolocationOptions
Optional parameters to customize the retrieval of the geolocation Position.
Options
enableHighAccuracy: Provides a hint that the application needs the best possible results. By default, the device attempts to retrieve a
Positionusing network-based methods. Setting this property totruetells 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.getCurrentPositionorgeolocation.watchPositionuntil the correspondinggeolocationSuccesscallback executes. If thegeolocationSuccesscallback is not invoked within this time, thegeolocationErrorcallback is passed aPositionError.TIMEOUTerror code. (Note that when used in conjunction withgeolocation.watchPosition, thegeolocationErrorcallback could be called on an interval everytimeoutmilliseconds!) (Number)maximumAge: Accept a cached position whose age is no greater than the specified time in milliseconds. (Number)
Android Quirks
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.
navigator.geolocation.clearWatch
Stop watching for changes to the device's location referenced by the watchID parameter.
Parameters
watchID: The id of the
watchPositioninterval to clear. (String)
Example
Objects (Read-Only)
Position
PositionError
Coordinates
Position
Contains Position coordinates and timestamp, created by the geolocation API.
Properties
coords: A set of geographic coordinates. (Coordinates)
timestamp: Creation timestamp for
coords. (DOMTimeStamp)
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
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)
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 navigator.geolocation.
Properties
code: One of the predefined error codes listed below.
message: Error message describing the details of the error encountered.
Constants
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 thetimeoutincluded ingeolocationOptions. When used withnavigator.geolocation.watchPosition, this error could be repeatedly passed to thegeolocationErrorcallback everytimeoutmilliseconds.
Sample: Get the weather, find stores, and see photos of things nearby with Geolocation
Use this plugin to help users find things near them such as Groupon deals, houses for sale, movies playing, sports and entertainment events and more.
Here's a "cookbook" of ideas to get you started. In the snippets below, we'll show you some basic ways to add these features to your app.
Get your geolocation coordinates
Get the weather forecast
Receive updated weather forecasts as you drive around
See where you are on a map
Both Bing and Google have map services. We'll use Google's. You'll need a key but it's free if you're just trying things out.
Add a reference to the maps service.
Then, add code to use it.
Find stores near you
You can use the same Google key for this.
Add a reference to the places service.
Then, add code to use it.
See pictures of things around you
Digital photos can contain geo coordinates that identify where the picture was taken.
Use Flickr API's to find pictures that folks have taken near you. Like Google services, you'll need a key, but it's free if you just want to try things out.
See Also:
Last updated
Was this helpful?