Redirect to location settings using cordova in android

12,222

You can achieve this using the cordova-diagnostic-plugin. Once installed, you call it via JS something like:

cordova.plugins.diagnostic.switchToLocationSettings();

UPDATE

You can use cordova-plugin-request-location-accuracy to request high accuracy location mode (i.e. GPS) directly from within the app. This will show a native confirm dialog and if user agrees, GPS will be enabled automatically with requiring user to manually change settings:

function onRequestSuccess(success){
    console.log("Successfully requested accuracy: "+success.message);
}

function onRequestFailure(error){
    console.error("Accuracy request failed: error code="+error.code+"; error message="+error.message);
    if(error.code !== cordova.plugins.locationAccuracy.ERROR_USER_DISAGREED){
        if(window.confirm("Failed to automatically set Location Mode to 'High Accuracy'. Would you like to switch to the Location Settings page and do this manually?")){
            cordova.plugins.diagnostic.switchToLocationSettings();
        }
    }
}

cordova.plugins.locationAccuracy.request(onRequestSuccess, onRequestFailure, cordova.plugins.locationAccuracy.REQUEST_PRIORITY_HIGH_ACCURACY);
Share:
12,222
Blessan Kurien
Author by

Blessan Kurien

Updated on June 09, 2022

Comments

  • Blessan Kurien
    Blessan Kurien almost 2 years

    Here is the requirements what i am trying to implement in my cordova android application

    1. When user enters the home page check to see if the gps is enabled or not.

    2. If not enabled i want to point the user to turn on the location settings.

    first part is made easily with GPS detector plugin and second part is implemented using the web intent plugin.But its not working as i expected.

    if(!gps){
         //gps is disabled try to show the location setting using webintent plugin
        window.plugins.webintent.startActivity(
            {
                action: window.plugins.webintent.ACTION_LOCATION_SOURCE_SETTINGS,
            },
            function() {},
            function() {
                alert('Failed to open URL via Android Intent.');
                console.log("Failed to open URL via Android Intent. URL: " + theFile.fullPath)
            }
        );              
    }
    

    I am getting this error Failed to open URL via Android Intent.

  • Fernando Fabreti
    Fernando Fabreti almost 9 years
    @Root, how did you achieve this?
  • Fernando Fabreti
    Fernando Fabreti almost 9 years
    Nevermind! Although it was a phonegap plugin with the last commit on Oct/13 it worked out of the box with Ionic 1.0.0
  • DaveAlden
    DaveAlden almost 9 years
    @FernandoFabreti FYI I recently forked that repo in order to add documentation and publish the plugin to npm and cordova registries - the forked repo is here
  • Fernando Fabreti
    Fernando Fabreti almost 9 years
    Great! I have noticed that Diagnostic.prototype.isGpsEnabled always returns true on my Galaxy S4 Device. Your doc says your 'similar' version returns true only on high precision enabled, have you changed that? Will take a look at your code, thanks.
  • DaveAlden
    DaveAlden almost 9 years
    I haven't changed the code in my fork - just added documentation based on the code comments, so it may be my docs are wrong.
  • Sudhakar Rapeti
    Sudhakar Rapeti about 8 years
    Thank you Very much helped me a lot