Redirect to location settings using cordova in android

前端 未结 1 1187
天命终不由人
天命终不由人 2021-01-03 02:18

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

相关标签:
1条回答
  • 2021-01-03 02:44

    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);
    
    0 讨论(0)
提交回复
热议问题