Capacitor LocationService Plugin
The LocationService plugin provides a way to track user location in your Capacitor-based application.
Features
Continuously track user location with high accuracy Receive location updates at regular intervals Easily request necessary location permissions Background location tracking support Lightweight and efficient implementation
Installation
npm install @your-org/capacitor-location-service
npx cap sync
Usage
1.Import the plugin in your Capacitor app: import { LocationService } from '@your-org/capacitor-location-service';
2.Request location permissions: await LocationService.requestPermissions();
3.Start the location service: await LocationService.startLocationService({ callbackId: 'locationUpdate' });
4.Add a listener to receive location updates:
LocationService.addListener('locationUpdate', (data) => {
console.log('Locations:', data.locations);
for(let location of data.locations) {
// example
yourApi.sendtoDatabase(location.latitude, location.longitude, location.timestamp);
}
});
5.Stop the location service when needed: await LocationService.stopLocationService();
API
startLocationService(...)
stopLocationService()
addListener('locationUpdate', ...)
removeAllListeners()
- Interfaces
startLocationService(...)
startLocationService() => Promise<void>
Start the location service.
options:
callbackId: The ID of the callback to receive location updates.
stopLocationService()
stopLocationService() => Promise<void>
Unregister the app from push notifications.
This will delete a firebase token on Android, and unregister APNS on iOS.
addListener(...)
addListener(eventName, listenerFunc) => Promise<void>
Add a listener for location updates.
options:
eventName: The name of the event to listen for ('locationUpdate'). listenerFunc: The function to call when the event is triggered.
removeAllListeners(...)
removeAllListeners() => Promise<void>
Remove all listeners for location updates.
options:
callbackId: The ID of the callback to receive location updates.
stopLocationService() Stop the location service. addListener(eventName, listenerFunc) Add a listener for location updates.
eventName: The name of the event to listen for ('locationUpdate'). listenerFunc: The function to call when the event is triggered.
removeAllListeners() Remove all listeners for location updates.
Example
Here's an example of how you can use the plugin:
import { LocationService } from '@your-org/capacitor-location-service';
async trackLocation() { await LocationService.startLocationService({ callbackId: 'locationUpdate' });
LocationService.addListener('locationUpdate', (data) => { console.log('Locations:', data.locations); for(let location of data.locations) { // example
yourApi.sendtoDatabase(location.latitude, location.longitude, location.timestamp); } }); }
async stopTracking() { await LocationService.stopLocationService(); LocationService.removeAllListeners(); }
Requirements
Capacitor 6.x Android 8.0 or higher
Supported Platforms
Android
Contributing
Contributions are welcome! Please follow the standard procedures for contributing to open-source projects.
Fork the repository
Create your feature branch (git checkout -b feature/my-new-feature) Commit your changes (git commit -am 'Add some feature') Push to the branch (git push origin feature/my-new-feature) Create a new Pull Request
License
This project is licensed under the MIT License.