@tenmednetwork/geolocation-tracker
v1.1.2
Published
Geolocation tracker module
Maintainers
Readme
geolocation-service
A simple JavaScript module to manage geolocation services with start, stop, and event listener capabilities.
start(config: object): void
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
configobject cannot be serialized to JSON.
stop(): void
Stops the geolocation service.
addLocationListener(listener: (event: LocationEvent) => void): EventSubscription
Adds a listener for geolocation updates.
- Parameters:
listener(function): Callback function invoked with aLocationEventobject when location updates occur.
- Returns: An
EventSubscriptionthat can be used to remove the listener.
addStatusListener(listener: (event: ChangeEvent) => void): EventSubscription
Adds a listener for status changes or completion events.
- Parameters:
listener(function): Callback function invoked with aChangeEventobject when the status changes or the service completes.
- Returns: An
EventSubscriptionthat can be used to remove the listener.
Usage Example
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-----`,
privateKey: `-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqh...
-----END PRIVATE KEY-----`,
certificate: `-----BEGIN 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();