FrancescoMussi
FrancescoMussi

Reputation: 21610

Ionic: geolocation not working on Android

THE SITUATION:

I am building an hybrid app with Ionic Framework. I need to get user location.

I found a cordova plugin called Geolocation

It works perfectly inside the browser, but it doesn't work testing the app in the phone (Android) or inside an emulator (Genymotion).

I know there are similar questions on SO but the solutions aren't working for me.

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:

As read in other SO answers, I have tried several other combinations regarding the options, but none of them did the trick.

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? What is the exact reason? How can i make it properly working?

Thank you!!

Upvotes: 0

Views: 6905

Answers (3)

Amit Patil
Amit Patil

Reputation: 3067

I was using cordova geolocation plugin.

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

And I was also facing the same problem, It wasn't working in genymotion. Setting enableHighAccuracy to true worked for me like below.

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

Upvotes: 1

ILYAS_Kerbal
ILYAS_Kerbal

Reputation: 1445

Try this: http://ionicframework.com/docs/v2/native/geolocation/ Please do not write the import statement.

Add Geolocation ##

$ ionic plugin add 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
})

Upvotes: 1

harshit
harshit

Reputation: 11

it depends on your cordova version. update it "npm update -g cordova" to latest . If this not work try using this method navigator.geolocation.getCurrentPosition.

Upvotes: 1

Related Questions