科尔多瓦地理位置插件不适用于Android [英] Cordova geolocation plugin doesn't work on android

查看:134
本文介绍了科尔多瓦地理位置插件不适用于Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用cordova,地理位置插件显示android上的经度和纬度。有很多相同的问题,所以我阅读并尝试他们的解决方案,但无法解决问题。我尝试了3种方法,第一种:navigator.geolocation.watchPosition,它在模拟器中返回错误的结果(37.42,-122.08)( 我还尝试了navigator.geolocation.getCurrentPosition,其中enableHighAccuracy设置为true和false我得到timeOut错误警报。



当我删除deviceready时,我没有得到timeOut,只是三种方法的结果都是错误的。



'+'Longitude:'+ position.coords.longitude +'\\\
'+'高度:'+ position.coords.altitude +'\ n'+'准确度:'+ position.coords.accuracy +'\'''''高度准确度:'+ position.coords.altitudeAccuracy +'\''''''标题:'+ position.coords.heading +' \\\
'+'速度:'+ position.coords.speed +'\\\
'+'时间戳:'+ position.timestamp +'\\\
'); };函数geolocationError2(错误){console.warn(检索当前位置时出错。+错误代码:+ error.code +,消息:+ error.message); } // 2 var onSuccess1 = function(position){alert('Latitude:'+ position.coords.latitude +'\\\
'+'Longitude:'+ position.coords.longitude +'\\\
'+'Altitude :'+ position.coords.altitude +'\\\
'+'准确度:'+ position.coords.accuracy +'\''+'高度准确度:'+ position.coords.altitudeAccuracy +'\''+ 'Heading:'+ position.coords.heading +'\\\
'+'Speed:'+ position.coords.speed +'\\\
'+'Timestamp:'+ position.timestamp +'\\\
'); };函数onError1(error){alert('code:'+ error.code +'\\\
'+'message:'+ error.message +'\\\
'+'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 +'\\\
'+'Longitude:'+ position.coords.longitude +'\\\
')};函数onError(error){alert('code:'+ error.code +'\\\
'+'message:'+ error.message +'\\\
'+'high accuracy:false'); } navigator.geolocation.getCurrentPosition(onSuccess,onError,{enableHighAccuracy:false,timeout:300 * 1000,maximumAge:0});

我从watchPosition获得了一部分内容另一个问题在stackoverflow。
我使用的是cordova版本:6.4.0。
我删除了插件并重试,但没有奏效。
我正在测试的设备是联想平板电脑。
非常感谢。



更新:我的Geolocation版本是:2.4.0它很重要吗?

解决方案

经过两天的努力,我开启了位置设置中的高精度。该应用程序现在工作!!!


I'm using cordova ,geolocation plugin for showing latitude and longitude on android. There are plenty of question same as this so I read and tried their solution but couldn't fix the problem. The code below works perfectly on browser.

I tried 3 method, first: "navigator.geolocation.watchPosition" which returns wrong result(37.42,-122.08) in emulator(Android Studio) and doesn't return anything in device.

I also tried "navigator.geolocation.getCurrentPosition" with both "enableHighAccuracy" set 'true' and 'false' and I get timeOut error alerted.

When I delete deviceready, I don't get the timeOut, just wrong result from all three methods.

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

I get the watchPosition part from another question in stackoverflow. I use cordova version: 6.4.0. I deleted the plugin and tried again but it didn't work. the device I'm testing with is a lenovo tablet. Thanks a lot.

UPDATE: my Geolocation version is: 2.4.0 Is it important???

解决方案

After two days of struggling I turned on "High accuracy" in location settings. The app Works now!!!

这篇关于科尔多瓦地理位置插件不适用于Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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