Location Services

๐Ÿ“˜

If your app requires access to the device location in Android, you will need to enable this permission and then call a JavaScript Bridge command to request permission at runtime. For iOS, permission is only prompted at runtime. But to avoid both the app and your website prompting twice for location permission, you will need to use the function median_geolocation_ready() which is called when location services are initialized within the app.

๐Ÿ‘

Developer Demo

Display our demo page in your app to test during development https://median.dev/location-services/

Avoid double location permission prompt on iOS

Your iOS app will override your website's geolocation permission prompt so that only the app will ask for permission, and a double permission prompt is avoided.

However, the web-based geolocation is overridden with the native one only after the webpage has loaded. So there is a potential timing issue if you call the JavaScript geolocation request before the native location services has been initialized.

The solution is that once the app is finished doing the native function binding, a JavaScript callback function median_geolocation_ready() is called within the web environment by the native layer of the app.

For example:

// Callback function automatically called when location services initialized in iOS app
function median_geolocation_ready() {
  navigator.geolocation.getCurrentPosition(locationSuccess, locationError, locationOptions);
}
        
// Use callback function as a helper function and call immediately if not in iOS app
if (!navigator.userAgent.includes('MedianIOS')) {
  median_geolocation_ready();
}

Prompt user to enable Location Services at runtime on Android

Ensure you have Location Services permission enabled in your app configuration, then prompt for permission at runtime by using the following Median JavaScript Bridge command.

โ†”๏ธMedian JavaScript Bridge

median.android.geoLocation.promptLocationServices();

If permission for location services has previously been granted by the user the above bridge command will have no effect. Otherwise the user will be shown a dialog asking them to provide permission.

Resources

Location Services Demo App
Background Location Native Plugin