如果 gps 关闭,离子显示错误

Ionic display error if gps is turned off

在 Ionic/Cordova 中尝试确定是否有人关闭了他们的 GPS 时遇到了一些问题。

这是我目前正在尝试做的事情,这在浏览器中可以正常工作,但在 android 上却不行。

navigator.geolocation.getCurrentPosition(function(pos) {}, function(err) {
    alert('We were unable to determine your location, please turn on your GPS/Location services');
});

另一件事是有人知道是否可以提示使用 Ionic/cordova 打开 GPS 吗?

有人有什么想法吗?

我认为使用 ngCordova 插件检测 GPS 状态更容易。 你可以试试这两个插件:

  1. $cordovaGeolocation:
    • 此插件使用 Wifi/3G/GPS 检测当前位置。
    • 它还有watch功能和错误回调。你应该用它来检查。
  2. $cordovaBackgroundGeolocation
    • 这与 $cordovaGeolocation 的工作原理相同,但具有 battery-saving 功能。
  3. 对于提示消息,我认为唯一的方法是通过Java代码编写的插件来完成。查看 Introduction to custom Cordova plugin development 了解更多详情。

请添加this插件,

cordova plugin add cordova.plugins.diagnostic

1.Determine 如果有人在 Ionic/Cordova

关闭了 GPS

Contoller

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

2.Giving提示开启GPS

Controller

if (window.cordova) {
    cordova.plugins.diagnostic.switchToLocationSettings();
}

试试这个

let optionsGPS = {timeout: 4000, enableHighAccuracy: true};
Geolocation.getCurrentPosition(optionsGPS).then((result) => {
  this.loadMap(result.coords.latitude, result.coords.longitude);
}).catch((err) => {
  let alert = this.alertCtrl.create({
        title: 'Error on GPS',
        subTitle: 'You need active the GPS',
        buttons: ['Ok']
    });
    alert.present();
});

我遇到了同样的问题,这个对我有用