首页 文章

离子:地理定位不适用于Android

提问于
浏览
0

THE SITUATION:

我正在使用Ionic Framework构建混合应用程序 . 我需要获得用户位置 .

我发现了一个名为Geolocation的cordova插件

它在浏览器中完美运行,但它无法在手机(Android)或模拟器(Genymotion)内测试应用程序 .

我知道在SO上有类似的问题,但解决方案对我不起作用 .

THE CODE:

var options = { timeout: 10000, enableHighAccuracy: true };

$cordovaGeolocation.getCurrentPosition(options).then(function(position)
{
    $scope.userLocation = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);

    $scope.map.markers = [{
        id: "first",
        location_data: {
        latitude: 45.3694868,
        longitude: -73.9803275
        }
    },{
        id: "second",
        icon: "img/blue_marker.png",
        location_data: {
        latitude: $scope.userLocation.H,
        longitude: $scope.userLocation.L
        }
    }];

}, function(error){

    alert('error geolocation');


});

AndroidManifest.xml中:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

ATTEMPTS:

正如在其他SO回答中所读到的那样,我已经尝试了几个关于选项的其他组合,但是他们都没有做到这一点 .

var options = {timeout: 15000, enableHighAccuracy: true, maximumAge: 5000};

var options = {timeout: 10000, enableHighAccuracy: false, maximumAge: 0};

var options = {timeout: 10000, enableHighAccuracy: true, maximumAge: 0};

ERROR MESSAGES IN CONSOLE:

k: no valid models attribute found 

TypeError: Cannot read property 'length' of undefined
at k.didQueueInitPromise 

TypeError: Cannot read property 'gManager' of undefined
at Object.fn 

TypeError: Cannot read property 'length' of undefined
at Object.fn

PositionError {message: "Timeout expired", code: 3, PERMISSION_DENIED: 1, POSITION_UNAVAILABLE: 2, TIMEOUT: 3}

THE QUESTION:

为什么Geolocation在浏览器中运行良好但在Android上运行不正常?究竟是什么原因?我怎样才能使它正常工作?

谢谢!!

3 回答

  • 1

    这取决于你的cordova版本 . 将它更新为“npm update -g cordova”到最新版 . 如果这不起作用,请尝试使用此方法navigator.geolocation.getCurrentPosition .

  • 1

    试试这个:http://ionicframework.com/docs/v2/native/geolocation/do not write import语句 .

    添加地理位置##

    $ ionic plugin添加cordova-plugin-geolocation

    如何使用它

    直接而无需导入 .

    Geolocation.getCurrentPosition().then((resp) => {
     //resp.coords.latitude
     //resp.coords.longitude
    })
    
    let watch = Geolocation.watchPosition();
    watch.subscribe((data) => {
     //data.coords.latitude
     //data.coords.longitude
    })
    
  • 0

    我使用的是cordova地理定位插件 .

    navigator.geolocation.getCurrentPosition(onSuccess,onError,options);
    

    我也遇到了同样的问题,它没有在genymotion中工作 . 将enableHighAccuracy设置为true对我有用,如下所示 .

    var options = {
      enableHighAccuracy: true,
      maximumAge: 0
    };
    

相关问题