Redirect to location settings using cordova in android

Here are the requirements I'm trying to implement in my cordova Android application

  • When a user enters the home page, check if gps is turned on or not.

  • If this is not allowed, I want to tell the user to enable location settings.

the first part is easily created using the GPS detector plugin, and the second part is implemented using the web intent plugin. But it does not work 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 get this error Failed to open URL via Android Intent.

+4
source share
1 answer

, cordova-diagnostic-plugin. JS :

cordova.plugins.diagnostic.switchToLocationSettings();

UPDATE

cordova-plugin-request-location-accuracy (, GPS) . , , GPS :

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);
+12

All Articles