Skip to main content
Version: v7

@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)

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.

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

ParamType
options
PositionOptions

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.

ParamType
options
PositionOptions
callback
WatchPositionCallback

Returns: Promise<string>

Since: 1.0.0


clearWatch(...)โ€‹

clearWatch(options: ClearWatchOptions) => Promise<void>

Clear a given watch

ParamType
options
ClearWatchOptions

Since: 1.0.0


checkPermissions()โ€‹

checkPermissions() => Promise<PermissionStatus>

Check location permissions. Will throw if system location services are disabled.

Returns:

Promise<PermissionStatus>

Since: 1.0.0


requestPermissions(...)โ€‹

requestPermissions(permissions?: GeolocationPluginPermissions | undefined) => Promise<PermissionStatus>

Request location permissions. Will throw if system location services are disabled.

Not available on web.

ParamType
permissions
GeolocationPluginPermissions

Returns:

Promise<PermissionStatus>

Since: 1.0.0


Interfacesโ€‹

Positionโ€‹

PropTypeDescriptionSince
timestampnumberCreation timestamp for coords1.0.0
coords{ latitude: number; longitude: number; accuracy: number; altitudeAccuracy: number | null; altitude: number | null; speed: number | null; heading: number | null; }The GPS coordinates along with the accuracy of the data1.0.0

PositionOptionsโ€‹

PropTypeDescriptionDefaultSince
enableHighAccuracybooleanHigh accuracy mode (such as GPS, if available) On Android 12+ devices it will be ignored if users didn't grant ACCESS_FINE_LOCATION permissions (can be checked with location alias).false1.0.0
timeoutnumberThe maximum wait time in milliseconds for location updates. In Android, since version 7.1.0 of the plugin, it is also used to determine the interval of location updates for watchPosition.100001.0.0
maximumAgenumberThe maximum age in milliseconds of a possible cached position that is acceptable to return01.0.0
minimumUpdateIntervalnumberThe minumum update interval for location updates. If location updates are available faster than this interval then an update will only occur if the minimum update interval has expired since the last location update. This parameter is only available for Android. It has no effect on iOS or Web platforms.50006.1.0

ClearWatchOptionsโ€‹

PropType
id
CallbackID

PermissionStatusโ€‹

PropTypeDescriptionSince
location
PermissionState
Permission state for location alias. On Android it requests/checks both ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION permissions. On iOS and web it requests/checks location permission.1.0.0
coarseLocation
PermissionState
Permission state for coarseLocation alias. On Android it requests/checks ACCESS_COARSE_LOCATION. On Android 12+, users can choose between Approximate location (ACCESS_COARSE_LOCATION) or Precise location (ACCESS_FINE_LOCATION), so this alias can be used if the app doesn't need high accuracy. On iOS and web it will have the same value as location alias.1.2.0

GeolocationPluginPermissionsโ€‹

PropType
permissionsGeolocationPermissionType[]

Type Aliasesโ€‹

WatchPositionCallbackโ€‹

(position: Position | null, err?: any): void

CallbackIDโ€‹

string

PermissionStateโ€‹

'prompt' | 'prompt-with-rationale' | 'granted' | 'denied'

GeolocationPermissionTypeโ€‹

'location' | 'coarseLocation'

Errorsโ€‹

The plugin returns specific errors with specific codes on native Android and iOS. Web does not follow this standard for errors.

The following table list all the plugin errors:

Error codePlatform(s)Message
OS-PLUG-GLOC-0002Android, iOSThere was en error trying to obtain the location.
OS-PLUG-GLOC-0003Android, iOSLocation permission request was denied.
OS-PLUG-GLOC-0004iOSThe 'getCurrentPosition' input parameters aren't valid.
OS-PLUG-GLOC-0005iOSThe 'watchPosition' input parameters aren't valid.
OS-PLUG-GLOC-0006iOSThe 'clearWatch' input parameters aren't valid.
OS-PLUG-GLOC-0007Android, iOSLocation services are not enabled.
OS-PLUG-GLOC-0008iOSApplication's use of location services was restricted.
OS-PLUG-GLOC-0009AndroidRequest to enable location was denied.
OS-PLUG-GLOC-0010AndroidCould not obtain location in time. Try with a higher timeout.
OS-PLUG-GLOC-0011AndroidTimeout needs to be a positive value.
OS-PLUG-GLOC-0012AndroidWatchId not found.
OS-PLUG-GLOC-0013AndroidWatchId needs to be provided.
OS-PLUG-GLOC-0014AndroidGoogle Play Services error user resolvable.
OS-PLUG-GLOC-0015AndroidGoogle Play Services error.
OS-PLUG-GLOC-0016AndroidLocation settings error.