如何自动开启GPS在Android? [英] How to Automatically turn ON GPS on Android?

查看:1412
本文介绍了如何自动开启GPS在Android?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何自动开启GPS或询问GPS的一个权限转时脚本运行的get经度和纬度的功能?

how to turn on GPS automatically or Ask a Permission turn on GPS when the script run function for get latitude and longitude ?

我在运行纹波

及其自动开启GPS在我的铬,但是当我建立在我的Andr​​oid智能手机上运行的GPS不会显示出来。

and its automatically turn on "GPS" on my chrome, but when i build and run in my Android Smartphone, the GPS wont show up.

我已经加上地理定位插件的配置,但仍然无法工作。

i already add "GeoLocation" plugin in config, but still wont work.

在此先感谢

推荐答案

应用程序不能自动打开Android上的GPS。它可以做的最好的是探测GPS是否开启/关闭,然后打开,如果后者的Andr​​oid位置设置页面。

An app can't automatically turn on GPS on Android. The best it can do is detect if GPS is switched on/off and then open the Android Location Settings page if the latter.

您可以使用这个插件要做到这一点,例如:

You can use this plugin to do this, for example:

cordova.plugins.diagnostic.isLocationEnabled(function(enabled){
    console.log("Location is " + (enabled ? "enabled" : "disabled"));
    if(!enabled){
        cordova.plugins.diagnostic.switchToLocationSettings();
    }
}, function(error){
    console.error("The following error occurred: "+error);
});

请注意,如果GPS关闭在Android上,地理位置API将返回超时错误,而不是拒绝的权限。如果使用添加的位置手表 navigator.geolocation.watchPosition() GPS时被关闭,一旦GPS开机时,你需要清除守望添加再次之前观察者将调用函数成功。

Note that if GPS is switched off on Android, the Geolocation API will return timeout error rather than permission denied. If a position watch was added using navigator.geolocation.watchPosition() while GPS was switched off, once GPS is switched on, you'll need to clear the watcher and add it again before the watcher will call the success function.

更新

您可以使用科尔多瓦 - 插件请求定位精度从应用程序内直接要求高精确度定位模式(即GPS)。这将显示本机确认对话框,如果用户同意,GPS会自动与要求用户手动更改设置中启用:

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

这篇关于如何自动开启GPS在Android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆