【问题标题】:Cordova geolocation plugin doesn't work on androidCordova 地理定位插件在 android 上不起作用
【发布时间】:2017-03-15 11:27:40
【问题描述】:

我正在使用 cordova 地理定位插件在 android 上显示纬度和经度。有很多与此相同的问题,因此我阅读并尝试了他们的解决方案,但无法解决问题。下面的代码在浏览器上完美运行。

我尝试了 3 种方法,首先:“navigator.geolocation.watchPosition”在模拟器(Android Studio)中返回错误结果(37.42,-122.08),并且在设备中不返回任何内容。

我还尝试了将“enableHighAccuracy”设置为“true”和“false”的“navigator.geolocation.getCurrentPosition”,但我收到了超时错误警报。

当我删除设备就绪时,我没有得到超时,只是所有三种方法的错误结果。

(function (window) {
  document.addEventListener("deviceready", onDeviceReady, false);
  function onDeviceReady() {
      
    var minAccuracyInMetres = 50;
    var positionWatcher;

    positionWatcher = navigator.geolocation.watchPosition(
      geolocationSuccess2,
      geolocationError2,
      { maximumAge: 0, timeout: 100000, enableHighAccuracy: true });

    function geolocationSuccess2(position) {
        // Reject if accuracy is not sufficient
        if (position.coords.accuracy > minAccuracyInMetres) {
            return;
        }

        // Only single position required so clear watcher
        navigator.geolocation.clearWatch(positionWatcher);

        alert('Latitude: ' + position.coords.latitude + '\n' +
                'Longitude: ' + position.coords.longitude + '\n' +
                'Altitude: ' + position.coords.altitude + '\n' +
                'Accuracy: ' + position.coords.accuracy + '\n' +
                'Altitude Accuracy: ' + position.coords.altitudeAccuracy + '\n' +
                'Heading: ' + position.coords.heading + '\n' +
                'Speed: ' + position.coords.speed + '\n' +
                'Timestamp: ' + position.timestamp + '\n');
    };

    function geolocationError2(error) {
        console.warn("Error while retrieving current position. " +
          "Error code: " + error.code + ",Message: " + error.message);
    }

    //2

    var onSuccess1 = function (position) {
        alert('Latitude: ' + position.coords.latitude + '\n' +
              'Longitude: ' + position.coords.longitude + '\n' +
              'Altitude: ' + position.coords.altitude + '\n' +
              'Accuracy: ' + position.coords.accuracy + '\n' +
              'Altitude Accuracy: ' + position.coords.altitudeAccuracy + '\n' +
              'Heading: ' + position.coords.heading + '\n' +
              'Speed: ' + position.coords.speed + '\n' +
              'Timestamp: ' + position.timestamp + '\n');
    };

    function onError1(error) {
        alert('code: ' + error.code + '\n' +
              'message: ' + error.message + '\n' + 'highaccuracy: true');
    }
    var options1 = { maximumAge: 0, timeout: 300000, enableHighAccuracy: true };
    navigator.geolocation.getCurrentPosition(onSuccess1, onError1, options1);
    //3
    var onSuccess = function (position) {
        alert('Latitude: ' + position.coords.latitude + '\n' +
              'Longitude: ' + position.coords.longitude + '\n')
    };

    function onError(error) {
        alert('code: ' + error.code + '\n' +
              'message: ' + error.message + '\n' + ' high accuracy:false');
    }

    navigator.geolocation.getCurrentPosition(onSuccess, onError, { enableHighAccuracy: false, timeout: 300 * 1000, maximumAge: 0 });
 }
})(window);

我从 stackoverflow 中的另一个问题中获得了 watchPosition 部分。 我使用科尔多瓦版本:6.4.0。 我删除了插件并再次尝试,但它没有工作。 我正在测试的设备是联想平板电脑。 非常感谢。

更新:我的地理位置版本是:2.4.0 重要吗???

【问题讨论】:

  • 希望您已启用 GPS
  • 是的,GPS 和 wifi 已启用。
  • 您尝试在哪部手机上运行应用程序?
  • 联想平板电脑型号:TAB 2 A8-50LC。安卓版本:5.0.2
  • 你能删除cordova-geo-location插件吗?使用相同的代码在移动应用程序中运行,无需插件。这样,它将回退到基于浏览器的地理位置。我认为它可能有效。

标签: javascript cordova geolocation


【解决方案1】:

经过两天的努力,我在位置设置中打开了“高精度”。该应用程序现在可以使用!!!

【讨论】:

  • 兄弟我已经尝试过但没有为我工作以及为什么我们需要在位置设置中使用高精度”。在我这边我已经检查了 GPS,并且在默认情况下检查 GPS 后““高精度”模式已选择。
猜你喜欢
  • 1970-01-01
  • 2016-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-12
  • 2015-01-11
相关资源
最近更新 更多