@ccervantesb/cordova-plugin-mock-location
v3.0.0
Published
This plugin allows checking the status from your current device location and detect if it's a spoof location. And create test location (Only for Android)
Maintainers
Readme
cordova-plugin-mock-location
This plugin allows checking the status of your current device location and detecting if it is a spoofed location. On Android, it can also create and update mock locations in real time.
Supported Platforms
| Method | Android | iOS |
|--------|:---:|:---:|
| check | ✅ | ✅ |
| set | ✅ | ❌ |
| update | ✅ | ❌ |
| disable | ✅ | ❌ |
| isAvailable | ✅ | ❌ |
| isDeveloperModeEnabled | ✅ | ❌ |
| requestDeveloperMode | ✅ | ❌ |
| requestPermissionMockLocation | ✅ | ❌ |
| requestBatteryOptimizationExclusion | ✅ | ❌ |
| requestPostNotifications | ✅ | ❌ |
| share | ✅ | ✅ |
Installation
cordova plugin add @ccervantesb/cordova-plugin-mock-locationOr from GitHub:
cordova plugin add https://github.com/cesarcervantesb/cordova-plugin-mock-locationPermissions
Android
The plugin automatically requests these permissions at runtime:
| Permission | Required for |
|------------|-------------|
| ACCESS_FINE_LOCATION | check, set, disable |
| ACCESS_COARSE_LOCATION | Fallback location permission |
| POST_NOTIFICATIONS (API 33+) | set (foreground service notification) |
| ACCESS_MOCK_LOCATION | set, update (debug builds only, must be granted manually in Developer Options) |
iOS
Add to your config.xml:
<edit-config target="NSLocationWhenInUseUsageDescription" file="*-Info.plist" mode="merge">
<string>Location access is required to detect mock locations.</string>
</edit-config>Developer Options (Android)
Before creating mock locations, go to Settings → Developer Options → Select mock location app and choose your application. The following helper methods can open the relevant settings screens:
await mockLocation.requestDeveloperMode(); // Opens "About phone"
await mockLocation.requestPermissionMockLocation(); // Opens "Developer options"Usage
The plugin creates the global objects cordova.plugins.mockLocation and mockLocation, accessible after the deviceready event.
document.addEventListener('deviceready', function () {
// mockLocation.* is now available
}, false);All methods return a Promise.
API Reference
check(getInfo?)
Detects whether the current device location is being mocked/spoofed.
Parameters:
| Param | Type | Default | Description |
|-------|------|---------|-------------|
| getInfo | Boolean | false | When true, includes mockLocation and gpsLocation objects if a mock is detected. |
Returns: Promise<Object>
| Field | Type | Description |
|-------|------|-------------|
| error | Boolean | false on success, true on failure |
| provider | String | Name of the location provider used for the check |
| isMockLocation | Boolean | true if the current location is mocked |
When getInfo = true and the location IS mocked:
| Field | Type | Description |
|-------|------|-------------|
| mockLocation | Object | The detected mocked location with latitude, longitude, accuracy, altitude, provider |
| gpsLocation | Object | The real GPS location with same fields (empty object if not available) |
On error:
| Field | Type | Description |
|-------|------|-------------|
| error | Boolean | true |
| code | String | Error code (e.g. "LOCATION_NOT_FOUND", "PERMISSION_DENIED") |
| message | String | Human-readable error message |
Example:
// Basic check
const result = await mockLocation.check();
console.log('Is mock?', result.isMockLocation);
// With detailed info
const detailed = await mockLocation.check(true);
if (detailed.isMockLocation) {
console.log('Mock coords:', detailed.mockLocation.latitude, detailed.mockLocation.longitude);
console.log('Real GPS:', detailed.gpsLocation);
}Platforms: Android ✅ · iOS ✅
set(opts)
Creates a fake GPS location using the Android test provider system. Starts a foreground service that continuously broadcasts the mocked coordinates.
Prerequisites: Developer Options must be enabled and your app must be selected under "Select mock location app". The app must be a debug build.
Parameters:
| Param | Type | Required | Default | Description |
|-------|------|----------|---------|-------------|
| opts.latitude | Number | ✅ | — | Fake latitude |
| opts.longitude | Number | ✅ | — | Fake longitude |
| opts.provider | String | ✅ | — | Unique name for this test provider |
| opts.showNotification | Boolean | ❌ | true | Whether to show a foreground notification |
| opts.notification | Object | ❌ | — | Custom notification configuration |
opts.notification object:
| Field | Type | Default | Description |
|-------|------|---------|-------------|
| importance | Number | IMPORTANCE_DEFAULT | Android notification importance level |
| name | String | "Default" | Notification channel name |
| channel_id | Number | — | Custom notification channel ID |
| title | String | "Simulating GPS location" | Notification title |
| description | String | "lat: ..., lng: ..." | Notification body text |
| titleActionBtn | String | "STOP FAKE GPS LOCATION" | Action button label |
| showProgress | Boolean | false | Show indeterminate progress bar |
Returns: Promise<Object> — Result of check(true) confirming the mock was set.
Example:
// Basic usage
await mockLocation.set({
provider: "berlin",
latitude: 52.520007,
longitude: 13.404954
});
// With custom notification
await mockLocation.set({
provider: "test",
latitude: 40.7128,
longitude: -74.0060,
showNotification: true,
notification: {
title: "GPS Simulator",
description: "Simulating location in NYC",
showProgress: true
}
});Platforms: Android ✅ · iOS ❌
update(opts)
Updates latitude, longitude, and/or provider name of an already-running mock location service in real time, without stopping and restarting the service.
Precondition:
setmust have been called first and the service must be running.
Parameters:
| Param | Type | Required | Description |
|-------|------|----------|-------------|
| opts.latitude | Number | ❌ | New latitude value |
| opts.longitude | Number | ❌ | New longitude value |
| opts.provider | String | ❌ | New provider name (recreates the test provider if changed) |
At least one field must be provided. The change takes effect immediately. The foreground notification text is also updated to reflect the new coordinates.
Returns: Promise<void>
Example:
// Start mock location
await mockLocation.set({
provider: "route",
latitude: 52.520007,
longitude: 13.404954
});
// Update only coordinates — service stays running
await mockLocation.update({ latitude: 52.5300, longitude: 13.4100 });
// Change everything including provider
await mockLocation.update({
latitude: 48.8566,
longitude: 2.3522,
provider: "paris"
});Platforms: Android ✅ · iOS ❌
disable(provider)
Stops the mock location foreground service and removes the test provider. Forces a network location refresh so the device falls back to real GPS.
Parameters:
| Param | Type | Required | Description |
|-------|------|----------|-------------|
| provider | String | ✅ | Name of the test provider to disable (same as used in set) |
Returns: Promise<Object> — Result of check(true) confirming the mock was removed.
Example:
await mockLocation.disable("test");Platforms: Android ✅ · iOS ❌
isAvailable()
Checks whether the device can create mock locations. Returns a comprehensive status object.
Returns: Promise<Object>
| Field | Type | Description |
|-------|------|-------------|
| isAvailableMockLocation | Boolean | true if the app has been granted "Allow mock locations" in Developer Options |
| isDeveloperModeEnabled | Boolean | true if Developer Options are enabled on the device |
| isIgnoringBatteryOptimizations | Boolean | true if the app is exempt from battery optimization |
| hasPermissionNotification | Boolean | true if notification permission is granted (or not required on older Android) |
Example:
const status = await mockLocation.isAvailable();
if (!status.isDeveloperModeEnabled) {
console.log('Developer Options must be enabled first');
} else if (!status.isAvailableMockLocation) {
console.log('Select this app under "Select mock location app"');
} else {
console.log('Ready to set mock locations');
}Platforms: Android ✅ · iOS ❌
isDeveloperModeEnabled()
Checks whether Developer Options are enabled on the device.
Returns: Promise<Object>
| Field | Type | Description |
|-------|------|-------------|
| isDeveloperModeEnabled | Boolean | true if Developer Options are enabled |
Example:
const { isDeveloperModeEnabled } = await mockLocation.isDeveloperModeEnabled();
if (!isDeveloperModeEnabled) {
console.log('Enable Developer Options by tapping "Build number" 7 times');
}Platforms: Android ✅ · iOS ❌
requestDeveloperMode()
Opens the device's "About phone" settings screen. The user must then tap "Build number" 7 times to unlock Developer Options.
Returns: Promise<void>
Example:
await mockLocation.requestDeveloperMode();Platforms: Android ✅ · iOS ❌
requestPermissionMockLocation()
Opens the device's "Developer options" settings screen. The user must select the app under "Select mock location app."
Returns: Promise<void>
Example:
await mockLocation.requestPermissionMockLocation();Platforms: Android ✅ · iOS ❌
requestBatteryOptimizationExclusion()
Opens the App Info settings screen so the user can disable battery optimization for your app. This prevents Android from killing the mock location foreground service.
Returns: Promise<void>
Example:
await mockLocation.requestBatteryOptimizationExclusion();Platforms: Android ✅ · iOS ❌
requestPostNotifications()
Requests the POST_NOTIFICATIONS permission. Required on Android 13+ (API 33) for the foreground service notification shown by set.
Returns: Promise<Object>
| Field | Type | Description |
|-------|------|-------------|
| granted | Boolean | true if the permission was granted (or not required on older Android) |
Example:
const result = await mockLocation.requestPostNotifications();
if (result.granted) {
console.log('Notifications permission granted');
}Platforms: Android ✅ · iOS ❌
share(opts)
Opens the native share dialog to share a Google Maps link of the given coordinates.
Parameters:
| Param | Type | Default | Description |
|-------|------|---------|-------------|
| opts.latitude | Number | 0 | Latitude to share |
| opts.longitude | Number | 0 | Longitude to share |
| opts.subject | String | "My current location" | Share subject (email) |
| opts.text | String | "My location" | Text accompanying the URL |
| opts.title | String | "Share my location with..." | Android chooser dialog title |
Returns: Promise<void>
Example:
await mockLocation.share({
latitude: 52.520007,
longitude: 13.404954,
text: "Check out this spot in Berlin!"
});Platforms: Android ✅ · iOS ✅
Troubleshooting
Mock location not working (Android)
- Ensure Developer Options are enabled —
isDeveloperModeEnabled()should returntrue - Under Developer Options → Select mock location app, select your application
- Verify with
isAvailable()—isAvailableMockLocationshould betrue - The app must be a debug build (release APKs cannot use
ACCESS_MOCK_LOCATION) - Disable battery optimization via
requestBatteryOptimizationExclusion()
Mock detection not working (iOS)
- iOS 15+ is required for reliable mock detection (
sourceInformation.isSimulatedBySoftware) - On iOS < 15,
isMockLocationalways returnsfalse - Ensure
NSLocationWhenInUseUsageDescriptionis configured inconfig.xml
Notification not showing (Android)
- On Android 13+ (API 33),
POST_NOTIFICATIONSpermission must be granted. The plugin requests it automatically when callingset. - You can also request it manually with
requestPostNotifications().
Complete Workflow Example
// Check if mock is active
const check = await mockLocation.check(true);
console.log('Mock active:', check.isMockLocation);
// Verify Android prerequisites
const available = await mockLocation.isAvailable();
console.log('Ready for mock:', available);
// Start mock location
await mockLocation.set({
provider: "route",
latitude: 37.7749,
longitude: -122.4194,
notification: {
title: "San Francisco",
description: "Simulating SF downtown"
}
});
// Simulate movement in real time
await mockLocation.update({ latitude: 37.7850, longitude: -122.4000 });
await mockLocation.update({ latitude: 37.8000, longitude: -122.4200 });
// Share current location
await mockLocation.share({
latitude: 37.7749,
longitude: -122.4194,
text: "My simulated location in SF"
});
// Disable mock
await mockLocation.disable("route");