简体   繁体   English

Ionic:地理位置无法在Android上运行

[英]Ionic: geolocation not working on Android

THE SITUATION: 情况:

I am building an hybrid app with Ionic Framework. 我正在使用Ionic Framework构建混合应用程序。 I need to get user location. 我需要获取用户位置。

I found a cordova plugin called Geolocation 我找到了一个名为Geolocation的cordova插件

It works perfectly inside the browser, but it doesn't work testing the app in the phone (Android) or inside an emulator (Genymotion). 它可以在浏览器中完美运行,但是无法在手机(Android)或模拟器(Genymotion)中测试该应用程序。

I know there are similar questions on SO but the solutions aren't working for me. 我知道在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: 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: 尝试:

As read in other SO answers, I have tried several other combinations regarding the options, but none of them did the trick. 正如在其他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: 问题:

Why Geolocation is working fine in the browser but not on Android? 为什么Geolocation在浏览器中可以正常运行,但在Android上却不能正常运行? What is the exact reason? 确切原因是什么? How can i make it properly working? 我如何使其正常工作?

Thank you!! 谢谢!!

it depends on your cordova version. 这取决于您的cordova版本。 update it "npm update -g cordova" to latest . 将其“ npm update -g cordova”更新为Latest。 If this not work try using this method navigator.geolocation.getCurrentPosition. 如果这不起作用,请尝试使用此方法navigator.geolocation.getCurrentPosition。

Try this: http://ionicframework.com/docs/v2/native/geolocation/ Please do not write the import statement. 尝试以下操作: http : //ionicframework.com/docs/v2/native/geolocation/不要编写 import语句。

Add Geolocation ## 添加地理位置##

$ ionic plugin add cordova-plugin-geolocation $ ionic插件添加cordova-plugin-geolocation

How to use it 如何使用它

Directly and without importing. 直接且无需导入。

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

I was using cordova geolocation plugin. 我正在使用cordova地理位置插件。

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

And I was also facing the same problem, It wasn't working in genymotion. 而且我也面临着同样的问题,它不适用于genymotion。 Setting enableHighAccuracy to true worked for me like below. 如下所示,将enableHighAccuracy设置为true对我有用。

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

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

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