简体   繁体   English

如何使用Cordova检测GPS是否开启

[英]how to detect GPS is on or off using cordova

I am creating mobile app which uses geolocation plugin and I want show popup to enable GPS when GPS is turned off. 我正在创建使用地理位置插件的移动应用,并且我想在GPS关闭时显示弹出窗口以启用GPS。 How to detect whether GPS is on or off and how to show location setting to on GPS? 如何检测GPS是否打开以及如何在GPS上显示位置设置?

It can be done using the following Cordova Plugin: https://github.com/dpa99c/cordova-diagnostic-plugin 可以使用以下Cordova插件完成此操作: https : //github.com/dpa99c/cordova-diagnostic-plugin

Add the plugin: 添加插件:

ionic plugin add cordova.plugins.diagnostic

Check for GPS whether it is enabled or not as below, 检查GPS是否已启用,如下所示,

cordova.plugins.diagnostic.isLocationEnabled(successCallback, errorCallback);

If GPS is not enabled, Open Location settings as below, 如果未启用GPS,则如下所示的“打开位置”设置,

cordova.plugins.diagnostic.switchToLocationSettings();

Complete code: 完整的代码:

cordova.plugins.diagnostic.isLocationEnabled(successCallback, errorCallback);
function successCallback(res){
  console.log("Location is " + (res ? "Enabled" : "not Enabled"));
  !res ? cordova.plugins.diagnostic.switchToLocationSettings() : '';
}
function errorCallback(err){
  console.log("Error: "+JSON.stringify(err));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM