Ionic Native Geolocation not working on Android
Asked Answered
D

4

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

Depressomotor answered 9/3, 2017 at 12:55 Comment(2)
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 nothingCicerone
@Cicerone sir i am also frustrated last 3 days in my case function is not trigged?Engler
C
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) => {
Cicerone answered 25/4, 2017 at 10:25 Comment(2)
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?Engler
This worked for me, without this option setting my App was silently failing in Android 12 without any exception(annoyingly..), thank you!Peper
V
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 :)

Vining answered 14/6, 2017 at 14:16 Comment(2)
Is it necessary to remove node_modules everytime the packages are updated ? lot time consuming & ambiguous thingPeper
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) linkVining
S
0

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

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

Stouthearted answered 28/12, 2017 at 5:18 Comment(0)
N
-1

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

Nikaniki answered 10/5, 2018 at 16:52 Comment(2)
Please provide some additional information, add some code and maybe a link to the docs.Dunbarton
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 onlyPeper

© 2022 - 2024 — McMap. All rights reserved.