How to: Get User's Location

Posted on 24. January 2023 by Jan Bunk


Our apps modify the navigator.geolocation JavaScript API to give you the most development capabilities and your app users the optimal experience.

You can use the geolocation JavaScript functions as normal, as long as you keep the limitations mentioned on this page in mind.

Additional Functions

openAppLocationSettings

This function opens the app info screen on Android and the settings App on iOS, where the user can enable location access for the app, even if he previously rejected the permission prompts.

You can find a code sample along with when it could make sense to call this function in the error handling section below.

You can listen to the appResumed event to detect when the user returned from the settings.

openDeviceLocationSettings

This function opens the settings app on Android where the user can enable location services. On iOS it also opens the settings app, but, due to technical limitations, not the exact page where location services are managed.

You can find a code sample along with when it could make sense to call this function in the error handling section below.

You can listen to the appResumed event to detect when the user returned from the settings.

Error Handling

There's a couple of potential error sources when getting the current location. We maintain compatibility with the original geolocation API but also provide additional information about what exactly went wrong.


function getAppErrorMessage(error) {
    if (error.hasOwnProperty("appMessage")) {
        return error.appMessage;
    }
    else {
        return null;
    }
}

var options = {
    enableHighAccuracy: true,
    timeout: 5000,
    maximumAge: 0
};

navigator.geolocation.getCurrentPosition(function(position) {
    // success
    console.log("Latitude: " + position.coords.latitude + ", Longitude: " + position.coords.longitude);
}, async function(error) {
    // error
    switch(error.code) {
        case error.PERMISSION_DENIED:
            // error.message is always "User denied Geolocation" to be consistent with browser behaviour
            console.log("User denied Geolocation");

            // Additional information only available for app users:
            if (getAppErrorMessage(error) === "Permission denied") {
                console.log("User did not grant the app permission to access the location, but we can try again later.");
            }
            else if (getAppErrorMessage(error) === "Permission denied permanently") {
                console.log("User did not grant the app permission to access the location and can only enable the permissions via the app settings.");

                // You can open the app settings like this:
                await openAppLocationSettings();
            }
            else if (getAppErrorMessage(error) === "Location service disabled") {
                console.log("User did not enable the location service setting in the device settings, even after being prompted to do so by the app.");

                // You can open the device's location settings like this:
                await openDeviceLocationSettings();
            }

            break;
        case error.POSITION_UNAVAILABLE:
            // can occurr in browsers with message "Unknown error acquiring position", but not used by the app at the moment
            console.log("Unknown error acquiring position");
            break;
        case error.TIMEOUT:
            // error.message is always "Position acquisition timed out" to be consistent with browser behaviour
            console.log("The request to get user location took longer than " + options.timeout + " milliseconds.");
            break;
    }

}, options);
    

Limitations

  • When you call navigator.geolocation.getCurrentPosition, you won't get back a GeolocationCoordinates or GeolocationPositionError object, so don't check it's type. You can still access all of the expected properties though.
  • If some properties are not supported by the device, e.g. GeolocationCoordinates.altitudeAccuracy, a browser would normally return null, while we might return 0, since that's what we get from the device.