Geolocation tracker module
npm install @tenmednetwork/geolocation-trackerA simple JavaScript module to manage geolocation services with start, stop, and event listener capabilities.
Starts the geolocation service with the specified configuration.
- Parameters:
- config (object): Configuration options for the geolocation service. This will be converted to JSON internally.
- Throws: Error if the config object cannot be serialized to JSON.
Stops the geolocation service.
Adds a listener for geolocation updates.
- Parameters:
- listener (function): Callback function invoked with a LocationEvent object when location updates occur.
- Returns: An EventSubscription that can be used to remove the listener.
Adds a listener for status changes or completion events.
- Parameters:
- listener (function): Callback function invoked with a ChangeEvent object when the status changes or the service completes.
- Returns: An EventSubscription that can be used to remove the listener.
``js
import GeoLocation from './GeoLocation';
// Start the geolocation service with configuration
GeoLocation.start(
{ interval: 60000 } // in milliseconds
);
GeoLocation.start({
interval: 60000, // in milliseconds
// mqtt configuration
mqtt: {
endpoint: 'your.endpoint.com',
topic: 'your/mqtt-topic',
rootCA: -----BEGIN CERTIFICATE-----
MIIDQTCCAimgAwIBAgI...
-----END CERTIFICATE-----,-----BEGIN PRIVATE KEY-----
privateKey:
MIIEvQIBADANBgkqh...
-----END PRIVATE KEY-----,-----BEGIN CERTIFICATE-----
certificate:
MIIDWTCCAkGgAwIBAgI...
-----END CERTIFICATE-----`
}
});
// Add a location listener
const locationSubscription = GeoLocation.addLocationListener({ location } => {
console.log('New location:', location);
});
// Add a status listener
const statusSubscription = GeoLocation.addStatusListener({ status } => {
console.log('Status changed:', status);
});
// To stop the service
GeoLocation.stop();
// To remove listeners when no longer needed
locationSubscription.remove();
statusSubscription.remove();