Ionic-native: geolocation.getCurrentPosition not work on android

Created on 11 Sep 2017  Â·  28Comments  Â·  Source: ionic-team/ionic-native

I'm submitting a ... (check one with "x")
[X ] bug report
[ ] feature request

Current behavior:
Use of geolocation.getCurrentPosition() dont work on android devices (tested on 2 diferents devices), but works fine on ionic serve.

Expected behavior:
Expected behavior is get current position, but it always return error number 3 (timeout error)

Steps to reproduce:
I do and clean ionic app with geolocation code:
https://github.com/macklus/ionic3-geolocation-demo

Download, run ionic cordova prepare and ionic cordova run android.
Even use --prod flag on ionic seems to do nothing

package.json info:

{
    "name": "ionic3-geolocation-demo",
    "version": "0.0.1",
    "author": "Ionic Framework",
    "homepage": "http://ionicframework.com/",
    "private": true,
    "scripts": {
        "clean": "ionic-app-scripts clean",
        "build": "ionic-app-scripts build",
        "lint": "ionic-app-scripts lint",
        "ionic:build": "ionic-app-scripts build",
        "ionic:serve": "ionic-app-scripts serve"
    },
    "dependencies": {
        "@angular/common": "4.1.3",
        "@angular/compiler": "4.1.3",
        "@angular/compiler-cli": "4.1.3",
        "@angular/core": "4.1.3",
        "@angular/forms": "4.1.3",
        "@angular/http": "4.1.3",
        "@angular/platform-browser": "4.1.3",
        "@angular/platform-browser-dynamic": "4.1.3",
        "@ionic-native/core": "3.12.1",
        "@ionic-native/geolocation": "^4.2.1",
        "@ionic-native/splash-screen": "3.12.1",
        "@ionic-native/status-bar": "3.12.1",
        "@ionic/storage": "2.0.1",
        "cordova-android": "^6.2.3",
        "cordova-plugin-compat": "^1.1.0",
        "cordova-plugin-console": "^1.0.5",
        "cordova-plugin-device": "^1.1.4",
        "cordova-plugin-geolocation": "^2.4.3",
        "cordova-plugin-splashscreen": "^4.0.3",
        "cordova-plugin-statusbar": "^2.2.2",
        "cordova-plugin-whitelist": "^1.3.1",
        "ionic-angular": "3.6.1",
        "ionic-plugin-keyboard": "^2.2.1",
        "ionicons": "3.0.0",
        "rxjs": "5.4.0",
        "sw-toolbox": "3.6.0",
        "zone.js": "0.8.12"
    },
    "devDependencies": {
        "@ionic/app-scripts": "2.1.4",
        "typescript": "2.3.4"
    },
    "description": "An Ionic project",
    "cordova": {
        "plugins": {
            "cordova-plugin-geolocation": {},
            "cordova-plugin-console": {},
            "cordova-plugin-device": {},
            "cordova-plugin-splashscreen": {},
            "cordova-plugin-statusbar": {},
            "cordova-plugin-whitelist": {},
            "ionic-plugin-keyboard": {}
        },
        "platforms": [
            "android"
        ]
    }
}

Most helpful comment

If you have gotten up to this point, ensure the following
ionic plugin add cordova-plugin-geolocation
then: npm install --save @ionic-native/geolocation
import { Geolocation } from '@ionic-native/geolocation'; to your *.ts file
And set this options

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

and have followed through all the above, @soromamadou solution worked for me. Restart the device.

All 28 comments

+1 test on Android Nexus 5X.
Also do not find the unsubscribe on type Observable<Geoposition>

@eunitedfr by now, i solve this problem by use map.getMyLocation() on Google Maps plugin, cause my app uses maps and geolocation together.

hope this help

Seems changing the GPS setting (settings->security->localisation->mode) from GPS to HighAccuracy (GPS+Wifi+phone,...) solve the problem. This isn't possible in a BtoC App, asking customer to change the settings on Android phone.
This look like a Cordova issue not a Ionic Native. I will search on the Cordova Jira if i found infos

For sure, this is a Cordova issue, so i close this issue.
I follow Jira task

Thanks!

Leaving this link here in case it helps others test Geolocation on Android: https://blog.ionicframework.com/testing-geolocation-on-android/

Try rebooting your device

If you have gotten up to this point, ensure the following
ionic plugin add cordova-plugin-geolocation
then: npm install --save @ionic-native/geolocation
import { Geolocation } from '@ionic-native/geolocation'; to your *.ts file
And set this options

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

and have followed through all the above, @soromamadou solution worked for me. Restart the device.

hi, any one hwo solved the problem, how can ionic3 geolocalisation will be able to get my geolocalisation for android !!?

How are you testing your app. If you are using the emulator, see this post
https://blog.ionicframework.com/testing-geolocation-on-android/

On Wed, Apr 18, 2018, 4:55 PM brahimi mohamed notifications@github.com
wrote:

hi, any one hwo solved the problem, how can ionic3 geolocalisation will be
able to get my geolocalisation for android !!?

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/ionic-team/ionic-native/issues/1958#issuecomment-382565319,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAgOAyELY64v5Ix03FhoUQ_lfTh5eZeXks5tp9KLgaJpZM4PTlET
.

Try Background Geolocation instead of Geolocation it solved my problem

@manish29ify
Background Geolocation is other geolocation type, and not really could be suitable for people need.
Both should work fine

I tried some of the recommendations above. Some of the solutions may have played a part in what eventually seemed to correct the issue on my Nexus 5 running 6.0.1 (at the time of this writing) .

The thing that seemed to finally correct the issue on this device was going to Settings > Location and turning the master toggle switch off and then back on. After I did this geolocation began working properly in the app I'm testing.

As recommended above, I also added:

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

// getCurrentPosition(options)

Side Notes:

  • My Nexus 7 (also running 6.0.1) never ran into this issue. Geolocation has worked in all tests. Neither has a phone plan, both are on wifi (just test devices).
  • I am testing using native binaries at this point, not through the Dev app. The Dev app has behaved fine all along.

We seem to have this problem more on samsung phones. I can't get anything to be consistent.
All these tests are on a Samsung S5 with Android 5.0 this phone seems to give us the most problems.
High Accuracy: true
timeout 20000

Dev build (no prod flag)
Everything worked fine. Found my location every time.

Same code, did a prod build (--prod flag)
Do the same test, I see an icon appear on the phone looking for location. Times out every time.
The phone is set to use High Accuracy (internet, gps, cell for location).

Another test with high accuracy turned off.
Probably a prod build.
Everytime fails. I turn wifi on and off works fine.

Reboot the phone, try again and it fails again.
Just not consistent in how its failing or working.

For this phone it doesn't seem like the timeout matters it either works or it doesn't from the start. It doesn't seem to matter if I wait 10 seconds or 20 seconds.

A theory from testing for the last 2 hours.
Just seems like the phone tries to get the location (however that is). And it fails. When the app asks for the location, it doesn't have an answer which fails. Until something is reset (maybe turning on and off wifi, sometimes turning on and off location). That only seems to get it to try to look for a location again, it could fail again.
The problem the phone hasn't gotten a location, it doesn't seem to matter what the app does its not going to get a good answer.

@newuser44 i dont think thats the problem.

Just for test, i check geolocation on:

  • Google Maps (fine)
  • native android app (fine)
  • getcapacitor app (fine)
  • cordova app (error)

Im sure problem is on cordova, and It was for a long time. Testing on diferents devices seems that problem is on some devices (BQ fails always, Samsung some). Maybe is relates on changes on OS, i dont know.

But i pretty sure problem is on cordova.

I think the different is cordova is using HTML5 to get the location. I'm guessing the others don't use that path. All cordova can do is ask for it, if the webview process is unable to get it from the phone (however that happens) things will fail.

The other apps I assume are going straight to the source, but I also assume that if its not getting a value it can somehow ask or request the data again. I'm guessing HTML5 can't or doesn't do that, and thus cordova can't.

I did find using enableHighAccuracy: true helps more because on the phone I can actually see it requesting the location. (something shows up in the status bar).

@manish29ify Thanks for this. I didn't realize Ionic had some of these plugins.
I had to clear out the android splash screen and images or it would fail to build.

In the app.
With Permission does a popup that says: plugin not installed
With Navigator does not have sufficient geolocation permissions.
With Navigator permissions nothing comes up.

Switched over to use just the HTML 5 version https://developer.mozilla.org/en-US/docs/Web/API/Geolocation_API

Which is all the plugin is doing anythings. I guess it gets straight to the source? In my case on the samsung S5 it still has the same problem finding location from the wifi.

The location will time out, I turned wifi on and off, it finds the location right away. Sometimes when I get signal, I'll close the app down all the way. Get back in and it gets stuck again getting the location.

Another thing I did was download this app
https://play.google.com/store/apps/details?id=ca.zyra.apps.ionicnativeplayground
This has a way to call the geoLocation library. Not surprising I get the same result on the same phone.

The only solution I have found is changing enableHighAccuracy: true This seems to force it to use the gps chip and not worry about the wifi location. (I don't know if it tries that first?)
My worry is that some phones gps might ended up being slower.

Sorry new theory. Tested this all again with wifi off. It should try to get location from the cell data.
This would all be with enableHighAccuracy: false.
Turn phone on
Go to app
App is able to pull location from web right away.
Leave screen and go back to pull location again, get a timeout.
Turn airplane mode on and off.
Go back to page, able to pull location again.

It seems like that call to geolocation might not be closing down as soon as I think.
It feels like when I do the second call, it doesn't work because the first call is still running somehow.
Turn off the network connections seems to force it to stop. The same thing happens if I'm on wifi.

Not sure how to prove this.

How are you testing your app. If you are using the emulator, see this post https://blog.ionicframework.com/testing-geolocation-on-android/
…
On Wed, Apr 18, 2018, 4:55 PM brahimi mohamed @.*> wrote: hi, any one hwo solved the problem, how can ionic3 geolocalisation will be able to get my geolocalisation for android !!? — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub <#1958 (comment)>, or mute the thread https://github.com/notifications/unsubscribe-auth/AAgOAyELY64v5Ix03FhoUQ_lfTh5eZeXks5tp9KLgaJpZM4PTlET .

This helped me. Using --livereload when runing the app on my device was the problem.

Try rebooting your device

It worked.
Bingo..

It's not working on Android 8.0.1 even by setting enableHighAccuracy to true settings not changed I have to manually select from the device. Is there any way which automatically changes the setting?

You had to add <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> line into AndroidManifest.xml file to access a permission for accessing geolocation

@apmeyer sir i got error after setTimeOut param in options :-
Error : position error :- timeout expired error every time
I have another major issue in the gelocation plugin : getCurrentPosition are not fire initially and i have trigger function only inside ionViewDidLoad() function why function not fire initially but logout then login again it will fire?

@here, Ionic 3. I did it this way and worked perfectly fine on both real device and browser

getUserLocation() {
this.platform.ready().then(() => {

  this.geolocation.getCurrentPosition({ enableHighAccuracy: true ,timeout:3000}).then((resp) => {
    this.currentLocation = { lat: resp.coords.latitude, lng: resp.coords.longitude }
    this.restProvider.currentLocation = this.currentLocation
    console.log("My location: ", this.currentLocation)

    this.map.setCenter(this.currentLocation)
    this.userMarker.setPosition(this.currentLocation)

  }).catch((error) => {
    console.log('Error getting location', error);
  });

});

}

Hi All,

this.geolocation.getCurrentPosition is not working if we minimize the app. I have enabled the ionic 4 background mode. SetTimeout for 1 mins. Every 1 min this.geolocation.getCurrentPosition function will get called. but I am not receiving location. But the same code is working in foreground. Not working in background.

Please help me to solve this issue

Was this page helpful?
0 / 5 - 0 ratings

Related issues

goleary picture goleary  Â·  3Comments

icchio picture icchio  Â·  3Comments

shaliko picture shaliko  Â·  3Comments

lycwed picture lycwed  Â·  4Comments

ajcrites picture ajcrites  Â·  3Comments