Skip to main content

Geolocation

These modules allow you to get the user's geolocation.

const coords: GeolocationCoordinates = {
latitude: -6.111232,
longitude: 106.9350912,
altitude: null,
accuracy: 921632.4265044113,
altitudeAccuracy: null,
heading: null,
speed: null,
};

Getting location at one particular time

there are two utilities for this:

  • getCurrentLocation — prompts the user to get geolocation permission (if the user hasn't granted permission), and then gets the current geolocation coordinate.

  • getCurrentLocationIfAllowed — gets the geolocation coordinate if the user has granted permission, otherwise directly returns null.

Getting real-time location

Use useCurrentLocation. It is updated every time the user moves.

import { useCurrentLocation } from '@traveloka/core';

function useMyHook() {
const { coords, error } = useCurrentLocation();
console.log(coords?.latitude);
}