Skip to main content

@napacitor/geolocation

GPS location and continuous tracking plugin (corresponds to @capacitor/geolocation).

Installation​

To use npm:

npm install @napacitor/geolocation

To use yarn:

yarn add @napacitor/geolocation

Sync native files:

npx nap sync

Usage​

import { Geolocation } from '@napacitor/geolocation';

// One-shot location
const position = await Geolocation.getCurrentPosition({
enableHighAccuracy: true,
timeout: 10000,
});
console.log(position.coords.latitude, position.coords.longitude);

// Permissions
const status = await Geolocation.checkPermissions();
await Geolocation.requestPermissions();

// Continuous tracking (receives Position or {code, message} errors)
const id = await Geolocation.watchPosition({}, (position, err) => {
if (err) { console.error(err); return; }
console.log(position?.coords);
});
await Geolocation.clearWatch({ id });

How It Works​

Wraps the APIs described in the .NET MAUI Geolocation and Permissions documentation: Geolocation.Default.GetLocationAsync (getCurrentPosition), StartListeningForegroundAsync / StopListeningForegroundAsync plus the LocationChanged / ListeningFailed events (watchPosition). Capacitor's callback-style watch is mapped to a "native watchPosition method + position / error event pipeline", and the entry layer wraps it into a Capacitor-shaped (options, callback) => Promise<CallbackID> signature. MAUI Geolocation only supports one foreground listening session; calling watchPosition again restarts it with the new parameters and returns a new id. Permissions map to Permissions.LocationWhenInUse (CheckStatusAsync / RequestAsync).

Permissions​

Android​

AndroidManifest.xml must declare location permissions (required by the MAUI documentation):

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

It is recommended to also declare the hardware features as optional to avoid Google Play filtering out devices without GPS:

<uses-feature android:name="android.hardware.location" android:required="false" />
<uses-feature android:name="android.hardware.location.gps" android:required="false" />
<uses-feature android:name="android.hardware.location.network" android:required="false" />

Or add equivalent [assembly: UsesPermission(...)] attributes in Platforms/Android/MainApplication.cs.

iOS​

Platforms/iOS/Info.plist must declare NSLocationWhenInUseUsageDescription (describing why the app needs location; if missing, the system denies the request outright). Mac Catalyst also needs Location Services enabled in Entitlements.plist.

Windows​

No additional configuration required.

API​

getCurrentPosition(options?)​

Gets the current position (one-shot). Throws ERROR when it cannot be obtained (possibly permission denied, location services disabled, or timeout).

ParamType
optionsPositionOptions

Returns: Promise<Position>

watchPosition(options, callback)​

Continuously watches position changes; returns a callback ID (for clearWatch). Calling again on the native side restarts the session with the new parameters and returns a new id; on web it goes straight to navigator.geolocation (ids get a web. prefix).

ParamType
optionsPositionOptions
callbackWatchPositionCallback

Returns: Promise<CallbackID>

clearWatch(options)​

Stops the given position watch (no-op when the id does not match).

ParamType
optionsClearWatchOptions

Returns: Promise<void>

checkPermissions()​

Checks location permissions (checks LocationWhenInUse).

Returns: Promise<PermissionStatus>

requestPermissions()​

Requests location permissions (platform prompt; unsupported on web).

Returns: Promise<PermissionStatus>

Interfaces​

GeolocationCoords​

PropTypeDescription
latitudenumberLatitude.
longitudenumberLongitude.
accuracynumberAccuracy (meters).
altitudenumber | nullAltitude (meters; null when not provided by the platform).
altitudeAccuracynumber | nullAltitude accuracy (meters; null when not provided by the platform).
headingnumber | nullHeading (0-360°; null when not provided by the platform).
speednumber | nullSpeed (m/s; null when not provided by the platform).

Position​

PropTypeDescription
timestampnumberCreation timestamp (Unix milliseconds).
coordsGeolocationCoordsCoordinates and accuracy.

PositionOptions​

PropTypeDescription
enableHighAccuracybooleanHigh accuracy mode (maps to MAUI GeolocationAccuracy.Best; default Default).
timeoutnumberMaximum wait time (milliseconds, default 10000; must be positive, otherwise INVALID_ARGUMENTS is thrown).
maximumAgenumberMaximum age of an acceptable cached position (milliseconds). Web-only; ignored on native.

ClearWatchOptions​

PropTypeDescription
idCallbackIDCallback ID returned by watchPosition.

PermissionStatus​

PropTypeDescription
locationPermissionStateLocation permission state (granted / denied / prompt; checks LocationWhenInUse).
coarseLocationPermissionStateCoarse location permission state. MAUI does not distinguish granularity, same value as location.