@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).
| Param | Type |
|---|---|
options | PositionOptions |
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).
| Param | Type |
|---|---|
options | PositionOptions |
callback | WatchPositionCallback |
Returns: Promise<CallbackID>
clearWatch(options)
Stops the given position watch (no-op when the id does not match).
| Param | Type |
|---|---|
options | ClearWatchOptions |
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
| Prop | Type | Description |
|---|---|---|
latitude | number | Latitude. |
longitude | number | Longitude. |
accuracy | number | Accuracy (meters). |
altitude | number | null | Altitude (meters; null when not provided by the platform). |
altitudeAccuracy | number | null | Altitude accuracy (meters; null when not provided by the platform). |
heading | number | null | Heading (0-360°; null when not provided by the platform). |
speed | number | null | Speed (m/s; null when not provided by the platform). |
Position
| Prop | Type | Description |
|---|---|---|
timestamp | number | Creation timestamp (Unix milliseconds). |
coords | GeolocationCoords | Coordinates and accuracy. |
PositionOptions
| Prop | Type | Description |
|---|---|---|
enableHighAccuracy | boolean | High accuracy mode (maps to MAUI GeolocationAccuracy.Best; default Default). |
timeout | number | Maximum wait time (milliseconds, default 10000; must be positive, otherwise INVALID_ARGUMENTS is thrown). |
maximumAge | number | Maximum age of an acceptable cached position (milliseconds). Web-only; ignored on native. |
ClearWatchOptions
| Prop | Type | Description |
|---|---|---|
id | CallbackID | Callback ID returned by watchPosition. |
PermissionStatus
| Prop | Type | Description |
|---|---|---|
location | PermissionState | Location permission state (granted / denied / prompt; checks LocationWhenInUse). |
coarseLocation | PermissionState | Coarse location permission state. MAUI does not distinguish granularity, same value as location. |