A Capacitor plugin for tracking location in the foreground and sending it to a server.
npm install foreground-location
npx cap sync
This plugin requires the following permissions in your AndroidManifest.xml:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<!-- Add the foreground service declaration -->
<service
android:name=".plugins.foregroundlocation.ForegroundLocationService"
android:enabled="true"
android:exported="false"
android:foregroundServiceType="location" />
NOTE: This plugin is written for testing purposes in line with my needs and currently only works with esri services. The service to be opened on the Esri side; Fields: objectid ( type: esriFieldTypeOID) x ( type: esriFieldTypeDouble ) y ( type: esriFieldTypeDouble) time ( type: esriFieldTypeDate) username ( type: esriFieldTypeString) and must be taking Point.
NOTE: The implementation working only android. Development required for iOS part.
import { ForegroundLocation } from 'foreground-location';
// Start the location service
async function startLocationTracking() {
try {
const result = await ForegroundLocation.startLocationService({
serverUrl: 'https://your-api.com/location',
accessToken: 'your-access-token',
updateInterval: 5000 // Optional, defaults to 5000ms (5 seconds)
});
console.log('Location tracking started:', result.message);
} catch (error) {
console.error('Error starting location tracking:', error);
}
}
// Stop the location service
async function stopLocationTracking() {
try {
const result = await ForegroundLocation.stopLocationService();
console.log('Location tracking stopped:', result.message);
} catch (error) {
console.error('Error stopping location tracking:', error);
}
}
echo(options: { value: string }) => Promise<{ value: string }>
Param | Type |
---|---|
options |
{ value: string } |
Returns: Promise<{ value: string }>
startLocationService(options: { serverUrl: string; accessToken: string; updateInterval?: number; }) => Promise<{ success: boolean; message: string; }>
Starts the foreground location service
Param | Type | Description |
---|---|---|
options |
{ serverUrl: string; accessToken: string; updateInterval?: number; } |
The server URL, access token, and update interval |
Returns: Promise<{ success: boolean; message: string; }>
stopLocationService() => Promise<{ success: boolean; message: string; }>
Stops the foreground location service
Returns: Promise<{ success: boolean; message: string; }>
MIT