Get the current geographic coordinates of the device.
The device's current latitude and longitude coordinates.
Simple usage
import { LocationModule, isSuccess, isErrorResponse } from '@grabjs/superapp-sdk';
// Initialize the location module
const location = new LocationModule();
// Get current coordinates
const response = await location.getCoordinate();
// Handle the response
if (isSuccess(response)) {
console.log('Coordinates:', response.result.lat, response.result.lng);
} else if (isErrorResponse(response)) {
switch (response.status_code) {
case 403:
console.log('No permission to access location');
// Trigger IdentityModule.authorize() for scope 'mobile.geolocation', then reload via ScopeModule.reloadScopes() and try again
break;
default:
console.error(`Error ${response.status_code}: ${response.error}`);
}
} else {
console.error('Unhandled response');
}
Get the country code based on the device's current location.
The ISO country code (e.g., 'SG', 'ID') based on the device's location.
Simple usage
import { LocationModule, isSuccess, isErrorResponse } from '@grabjs/superapp-sdk';
// Initialize the location module
const location = new LocationModule();
// Get country code
const response = await location.getCountryCode();
// Handle the response
if (isSuccess(response)) {
console.log('Country code:', response.result.countryCode);
} else if (isErrorResponse(response)) {
switch (response.status_code) {
case 403:
console.log('No permission to access location');
// Trigger IdentityModule.authorize() for scope 'mobile.geolocation', then reload via ScopeModule.reloadScopes() and try again
break;
default:
console.error(`Error ${response.status_code}: ${response.error}`);
}
} else {
console.error('Unhandled response');
}
Subscribe to location change updates from the device.
A BridgeStream that emits location updates as the device location changes.
Use subscribe() to listen for updates, or await to get the first value only.
This method returns a BridgeStream that continuously emits location updates.
Remember to call unsubscribe() on the subscription when you no longer need updates
to conserve battery and free resources.
Simple usage
import { LocationModule, isSuccess, isErrorResponse } from '@grabjs/superapp-sdk';
// Initialize the location module
const location = new LocationModule();
// Subscribe to location changes
const subscription = location.observeLocationChange().subscribe({
next: (response) => {
if (isSuccess(response)) {
console.log('Location updated:', response.result.lat, response.result.lng);
} else if (isErrorResponse(response)) {
switch (response.status_code) {
case 403:
console.log('No permission to access location');
// Trigger IdentityModule.authorize() for scope 'mobile.geolocation', then reload via ScopeModule.reloadScopes() and try again
break;
default:
console.error(`Error ${response.status_code}: ${response.error}`);
}
}
},
complete: () => console.log('Location stream completed')
});
// Later, to stop receiving updates:
subscription.unsubscribe();
JSBridge module for accessing device location services.
Remarks
Provides access to the device's geolocation data including coordinates and country code. This code must run on the Grab SuperApp's WebView to function correctly.
Example
ES Module:
Example
CDN (UMD):