Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic Native Geolocation not working on Android

I have the following code using Ionic Native Geolocation:

import { Geolocation } from 'ionic-native';

    this.platform.ready().then(() => {
        alert('loadMap about to getCurrentPosition');
          Geolocation.getCurrentPosition(options).then((position) => {
        alert('loadMap getCurrentPosition');
            let latLng: google.maps.LatLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
            bound.extend(latLng);
            this.load(bound);
          });
    });

When I run this as ionic serve in a browser, or I build it and run it on iOS Simulator (Xcode), it works. However, when I build this for Android, and try run it, the first alert gets fired, but not the second.

That means for Android only, Geolocation.getCurrentPosition... is not working.

I do have another page that can render a map via this.map = new google.maps.Map(htmlElement, mapOptions);, so it looks like the issue is with getting the current position. When I install the app, I do get this message however:

Allow AppName to access the device's location?
DENY    ALLOW

To which I click Allow.

Does anyone know what I am doing incorrectly, or if there are some missing steps in the Android install and build process?

Thanks

like image 954
Richard Avatar asked Mar 09 '17 12:03

Richard


2 Answers

Ok, so I have struggled with this for a while now. Some times it works, some times it does not. Try to add options to it:

let options = {timeout: 10000, enableHighAccuracy: true, maximumAge: 3600};
Geolocation.getCurrentPosition(options).then((resp) => {
like image 121
Harry Avatar answered Nov 11 '22 16:11

Harry


These following steps did the trick for me:

Install the two latest ionic-cli plugins:

sudo npm install -g ionic@latest
npm install --save-dev --save-exact @ionic/cli-plugin-cordova@latest
npm install --save-dev --save-exact @ionic/cli-plugin-ionic-angular@latest

Then install the geolocation plugin:

npm install @ionic-native/geolocation --save
ionic cordova plugin add cordova-plugin-geolocation

Then remove the '/node_modules' folder and clean the cache of npm:

rm -rf node_modules/
npm cache clean --force

And finally reinstall the packages:

npm install

Hope it will help someone :)

like image 3
Nicolas Avatar answered Nov 11 '22 16:11

Nicolas