@capacitor/geolocation
The Geolocation API provides simple methods for getting and tracking the current position of the device using GPS, along with altitude, heading, and speed information if available.
Install
npm install @capacitor/geolocation
npx cap sync
iOS
Apple requires privacy descriptions to be specified in Info.plist for location information:
NSLocationAlwaysAndWhenInUseUsageDescription(Privacy - Location Always and When In Use Usage Description)NSLocationWhenInUseUsageDescription(Privacy - Location When In Use Usage Description)
This Capacitor plugin does not support background geolocation directly. However, it relies on
ion-ios-geolocation, which can report
location in the background. As a result, Apple requires you to include a
NSLocationAlwaysAndWhenInUseUsageDescription entry in your Info.plist. Since this permission
prompt won’t appear to users, you can safely use the same description string as for
NSLocationWhenInUseUsageDescription.
Read about Configuring Info.plist in the iOS Guide for more information on setting iOS permissions in Xcode
Android
This plugin requires the following permissions be added to your AndroidManifest.xml:
<!-- Geolocation Plugin -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature android:name="android.hardware.location.gps" />
The first two permissions ask for location data, both fine and coarse, and the last line is optional but necessary if your app requires GPS to function. You may leave it out, though keep in mind that this may mean your app is installed on devices lacking GPS hardware.
If you only require approximate location (variable accuracy but usually around 2 kilometers), you may just declare ACCESS_COARSE_LOCATION and <uses-feature, and use enableHighAccuracy=false when requesting location
Read about Setting Permissions in the Android Guide for more information on setting Android permissions.
API
For list of error codes, see Errors
getCurrentPosition(...)
getCurrentPosition(options?: PositionOptions | undefined) => Promise<Position>
Get the current GPS location of the device
| Param | Type |
|---|---|
options | |
Returns:
Promise<Position>
Since: 1.0.0
watchPosition(...)
watchPosition(options: PositionOptions, callback: WatchPositionCallback) => Promise<CallbackID>
Set up a watch for location changes. Note that watching for location changes can consume a large amount of energy. Be smart about listening only when you need to.
| Param | Type |
|---|---|
options | |
callback | |
Returns: Promise<string>
Since: 1.0.0
clearWatch(...)
clearWatch(options: ClearWatchOptions) => Promise<void>
Clear a given watch
| Param | Type |
|---|---|
options | |
Since: 1.0.0