7

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

Richard
  • 8,193
  • 28
  • 107
  • 228
  • I have been using this for a while and it worked. Now suddenyl it does not work anymore. No code changes. I think your code is fine, but I think somethone else is unstable. SOOOOOOO frustrating. No time out nothing – Harry Apr 25 '17 at 10:16
  • @Harry sir i am also frustrated last 3 days in my case function is not trigged? – Kapil Soni Apr 29 '19 at 12:26

4 Answers4

8

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) => {
Harry
  • 13,091
  • 29
  • 107
  • 167
  • Sir i have tried above code but not working for me getCurrentPosition function not fire for me it working only after run android but after generate build its not working tell me sir what's the problem? – Kapil Soni Apr 29 '19 at 12:41
  • This worked for me, without this option setting my App was silently failing in Android 12 without any exception(annoyingly..), thank you! – Naga Jan 27 '22 at 17:28
3

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 :)

Nicolas
  • 2,684
  • 1
  • 16
  • 22
  • Is it necessary to remove node_modules everytime the packages are updated ? lot time consuming & ambiguous thing – Naga Jan 27 '22 at 16:06
  • You can do `npm update`. To check if any packages in your Node.js project are outdated, run `npm outdated` in the root folder (where the package.json file is). This command will output the current installed versions of all packages, the wanted version (`npm update` would want to update to this version) [link](https://www.mariokandut.com/how-update-node-dependency-npm/) – Nicolas Feb 01 '22 at 19:58
0

let options = {timeout: 10000, enableHighAccuracy: true, maximumAge: 3600};

this seem run the geolocation but can not catch the lat and lng return {}

-1

Try Background Geolocation instead of Geolocation it solved my problem for more detail read the document https://ionicframework.com/docs/native/background-geolocation/

  • 1
    Please provide some additional information, add some code and maybe a link to the docs. – Armin Šupuk May 10 '18 at 17:21
  • Your App may get rejected or can be removed if you dont give proper reasoning for background location usage. lets stick to the question which is foreground usage only – Naga Jan 27 '22 at 16:03