apache / cordova-plugin-geolocation

Apache Cordova Geolocation Plugin
https://cordova.apache.org/
Apache License 2.0
633 stars 657 forks source link

Not working on Android devices #114

Closed sachinGAcharya closed 3 years ago

sachinGAcharya commented 5 years ago

$ ionic cordova plugin add cordova-plugin-geolocation --variable GEOLOCATION_USAGE_DESCRIPTION="To locate you" $ npm install --save @ionic-native/geolocation

I used the above plugin referred in https://ionicframework.com/docs/native/geolocation/ But I am not getting the cureent location on android devices.Many people have the same issue

https://forum.ionicframework.com/t/ionic-get-current-location/118168/6 tried using running the app using prod flag dint help.

please help me

JaviRpo commented 5 years ago

The same for me, the plugin doesn't work

SimonSoft93 commented 5 years ago

The same for me, on Android doesn't work

alessandrobarbetti commented 5 years ago

Same problem for me, plugin not working on Android 8.0 Tested on app using watchPosition function and latest version of plugin. Works fine on all devices up to Android 7.0 ( not tested on 7.1). On two different devices with android 8.0 watchPosition start fine then stops after 120 seconds.

battika commented 5 years ago

Correct, stopped working on Android Oreo.

natedl98 commented 5 years ago

Not working for me either

lumitor2 commented 5 years ago

Same for me on Oreo and Kit Kat also

ArGaspar commented 5 years ago

Is this plugin getting updated for android 8.0 or do we have to move on?

davidgovea commented 5 years ago

Working fine for me on 8.0, and 8.1 devices.

However, I am experiencing intermittent timeout issues on some devices (which all happen to be Samsungs -- but not every samsung has issues).

I suspect that it's a permission/system issue related to Samsung's mangling of android OS. Try opening google maps, acquiring position, and then try again. Obviously, this is not a fix, but I don't think the blame sits with this library.

sachinGAcharya commented 5 years ago

@davidgovea

I have experienced this issue in MI A1,and tab ,I am not getting the current location.I tried high accuracy settings but no use.I dont know how you are able to get the location on phone. When I run ionic serve and check on the console I am getting the location But not in Android hardware devices .

vancuren commented 5 years ago

Same issue. It's intermittently on S9 and Note 8 devices.

fernandoocf commented 5 years ago

Same here.... On getCurrentPosition I never receive a callback.... If I go to mobile location settings and set the location method from "mobile only" to "High Precision" it resolves the problem.....

But when I try to do:

this.geolocation.getCurrentPosition( {enableHighAccuracy: true}).then(callback => {.....}) I never receive the callback.......

iOS works fine for me.

marteagam commented 5 years ago

Same issue. I never receive the callback.

sachinGAcharya commented 5 years ago

Thanks for supporting all. It is very urgent can this issue will be fixed or we have to move on?.

vwasteels commented 5 years ago

+1

janpio commented 5 years ago

Has anyone created a pure Cordova reproduction of the error yet as a Github repository? If not, please one of you having the issue do so (start with cordova create, commit to a Github repository) and document all the commands you execute and changes you do and commit each step individually so it is easy to follow. Then post the URL here, then it is easier for someone to reproduce.

What exact versions of Android are affected? What exact devices are affected? Is the issue reproducible in an emulator?

fernandoocf commented 5 years ago

@janpio try to reproduce on Samsung Galaxy S7 - Android 8.0.0

This is the device model that I use to test my app. You will probably get the same issue on this device.

davidgovea commented 5 years ago

If you're experiencing this issue, make sure you are providing the timeout option to your geolocation call, e.g. getCurrentPosition(successFn, errorFn, { timeout: 10000 });

This way, failing devices can be handled in the same way as a user who declines location permissions (which your app should already do), instead of hanging.


I would say it's an extremely good bet that this issue will be resolved, but perhaps not in the timescale that you would prefer. If the issue is highly urgent, I might suggest the following approaches for your consideration (ranked from helpful to not-so-helpful):

  1. Create a reproduction project as @janpio has suggested
  2. Invest some time into fixing/updating this free, open-source code
  3. Provide more testing anecdotes and +1's to this thread
  4. Find a different approach ("move on")
  5. Motivate the contributors by threatening (or even just bluffing) to "move on" 😉
vwasteels commented 5 years ago

Tested with Android version 6.0

git clone git@github.com:vwasteels/test-cordova-geolocation.git
cd test-cordova-geolocation
cordova prepare
cordova run android --device

open inspector at chrome://inspect console says, after 10 seconds :

Received Event: deviceready
getCurrentPosition fails
PositionError {code: 3, message: "Timeout expired"}
janpio commented 5 years ago

Thanks @vwasteels!

I have a Google Nexus 5, running Android 6.0.1 (October 5, 2016 security patch level). I cloned your repo, run the commands and tested on that device. On startup I got the usual question for location permission, which I of course agreed. Then in chrome://inspect it successfully receives my location:

image

(The repo uses Cordova Android 7.0.0 by the way. You might want to try to upgrade the platform to 7.1.1 with cordova platform rm android then cordova platform add android@7.1.1 - although I don't expect this to change anything.)

So I can't reproduce on my Android 6.x device.

vwasteels commented 5 years ago

@janpio thanks for testing, I also tried upgrading cordova-android, but it doesn't change anything. and I was also prompted for location permission.

Can you guys test my repo with your device and log the result here ?

thank you

sachinGAcharya commented 5 years ago

@janpio please share your repo link

janpio commented 5 years ago

I didn't create an additional repo, I just checked out what @vwasteels posted above in https://github.com/apache/cordova-plugin-geolocation/issues/114#issuecomment-421491252. The https URL is https://github.com/vwasteels/test-cordova-geolocation

lumitor2 commented 5 years ago

I've just tested @vwasteels repo, but there is some syntax error. :-/ snimek obrazovky 2018-09-17 v 10 22 18

janpio commented 5 years ago

Seems the device's webview you are using doesn't understand arrow functions: https://github.com/vwasteels/test-cordova-geolocation/blob/ebe675917d9d3f1f06e91cfc1d6a59259c475e0f/www/js/index.js#L32 Post your device information an Android version for context @lumitor2

vwasteels commented 5 years ago

@lumitor2 I just updated the repo , I removed arrow functions

sachinGAcharya commented 5 years ago

I just removed the plugin and added again, Then removed the Android platform and added again, I am now getting the location on MI phones. posting here so it may help someone. Thank you,

lumitor2 commented 5 years ago

@vwasteels thanks for update. Now I was successful to build it on Lenovo A536 (Android v. 4.4.2) and Geolocation failed. :-/ There is same problem with timeout. snimek obrazovky 2018-09-17 v 13 59 56 @sachinGAcharya This is weird. I have tried to change plugin and also cordova-android versions, but with no difference. Also direct access with navigator.geolocation did not work.

janpio commented 5 years ago

@lumitor2 I guess for Android 4.x/KitKat you better open a new issue - most of the reports here are about 8.x/Oreo. Will be better to debug this separately, especially with "direct access with navigator.geolocation did not work". Just include the link to @vwasteels' repro repo as well and the images you posted here. Thanks.

fernandoocf commented 5 years ago

Here is my solution:

You will need use: Platform: https://ionicframework.com/docs/api/platform/Platform/ Diagnostic: https://ionicframework.com/docs/native/diagnostic/ Geolocation: https://ionicframework.com/docs/native/geolocation/
Location Accuracy: https://ionicframework.com/docs/native/location-accuracy/

getUserPosition() {
    const HIGH_ACCURACY = 'high_accuracy'';
    if (this.platform.is('cordova')) {
      this.diagnostic.isLocationEnabled().then(enabled => {
        if (enabled) {
          this.diagnostic.getLocationMode().then(locationMode => {
            if (locationMode === HIGH_ACCURACY) {
              this.geolocation.getCurrentPosition().then((position) => {
                // populate my data
              });
            } else {
                this.askForHighAccuracy();
            }
          });
        } else {
          // handle if user deny location 
        }
      });
    } 
  }

askForHighAccuracy() {
 this.locationAccuracy
   .request(this.locationAccuracy.REQUEST_PRIORITY_HIGH_ACCURACY).then(() => {
     this.geolocation.getCurrentPosition().then((position) => {
       // populate my data
     });
   });
}

After this I need that the user set the High Accuracy mode and accept google terms to use.

Hope it work for you guys =)

lumitor2 commented 5 years ago

@janpio I have same problem on #Android 7 and 8. I can try it tomorrow to let you know, if @vwasteels repo works there. @fernandoocf so you use this plugin? https://ionicframework.com/docs/native/diagnostic/

fernandoocf commented 5 years ago

@lumitor2 Yes! I'll edit my post with all specifications.

battika commented 5 years ago

if (locationMode.HIGH_ACCURACY)

Are you sure this line is doing what it is supposed to be doing? According to the documentation one should do the comparison of the returned location mode value to the location mode constant values. One more thing is I believe isLocationAuthorized() should also be checked before concluding location can be queried.

Either way the provided example did not work on my Xiaomi MI A2 running the latest Oreo release but it bailed out with an exception when called

 this.geolocation.getCurrentPosition().then((position) => {

so unfortunately it did not help :(

fernandoocf commented 5 years ago

Code updated!

I can't understand why It won't works on your Xiaomi, because LocationAccuracy plugin use a native call from device. I used Uber's same approach and worked on my Samsung S7.... Now I'm curious to check it on Xiaomi devices. I'll do it tomorrow!

I try my solution on Samsung S7 (Android 8.0.0), Moto X2 (Android 6.0.0) and it work's

Thanks for the feedback @battika =)

fernandoocf commented 5 years ago

Hey @battika

My solution works too on Xiaomi Note 5 (Android Oreo 8.1.0).

gustavogiancoli commented 5 years ago

Same problem for me. However it is a problem of some mobiles, may work in many S7, Moto G and others not(My specifc problem its of some S7 models). I offer to my users to change the setting to high accuracy and solve that problem while the developer not analysis it.

vwasteels commented 5 years ago

My phone has High accuracy mode on, the problem remains the same for me.

vwasteels commented 5 years ago

Location is enabled :

capture d ecran 2018-09-21 a 15 36 35

Mode is set to HIGH :

capture d ecran 2018-09-21 a 15 36 03

Still got the timeout error code 3 :

capture d ecran 2018-09-21 a 15 38 01

I'm available on Skype, we can share screen to reproduce together or whatever, personally I don't know a thing about Android native coding, nor iOs...

katk0smos commented 5 years ago

Also not working for me on Android 9.0 on a Google Pixel 2 XL.

lorenzyannick commented 5 years ago

I tried with the repo above.

oneplus 5T : has High accuracy mode on, the problem remains the same (Android 8.1 ; Android platform 7+) Xiaomi redmi 4 : has High accuracy mode on, the problem remains the same (Android 6.0.1 ; Android platform 7+ AND android 6.4.0)

Mijue M6 : geoloc works (Android 4.4.2 ; android platform 6.4.0) I remove the cordova-plugin-geolocation plugin and it's not working anymore on my M6 (geoloc fails !) I'll check if this plugin cordova-plugin-geolocation map the js function correctly. It seems that when it use the webview function navigator.geolocation.getCurrentPosition it's not working. Maybe check that navigator.geolocation.getCurrentPosition is really calling a native code (and not browser js function)

On iOS, it's working with the same code. I don't how to diagnostic more. I'm stuck

StevenMapes commented 5 years ago

I had the same problem with Phonegap and every version of this plugin since 2.4.3 (tested on a OnePlus5). Though even that version is incredibly unstable when it comes to gathering location data. Sometimes it will work first time, then a minute later you can have it try and retry upon failure (high, then low accuracy with 30 second, 20 second, 15 second, 10 second, 5 second) timeouts and it'll fail all 5 times so a total of 1 minute 20 seconds of trying.

It's extremely frustrating as it use to just work

Ravenstine commented 5 years ago

On my Samsung Galaxy S5, the issue has to do with Wi-Fi scanning being enabled. Turning this feature off immediately allows the geolocation to work.

mlynch commented 5 years ago

fwiw, the webview implementation of geolocation on Android does not function properly, as I found building the Geo plugin for capacitor. I think the only solution is to go back to the native Geolocation implementation.

darkguy2008 commented 5 years ago

@mlynch okay, so how does one get back to the native Geolocation implementation? Any pointers?

dudizimber commented 5 years ago

I have an Asus Zenfone 3 Zoom, running 8.0.0, and after some tests, I saw that for some reason, to the Geolocation plugin to work, you have to enable WiFi and/or Bluetooth verification on Localization settings. Not sure why, but it worked for me...

mlynch commented 5 years ago

@darkguy2008 this plugin will have to add back the native geolocation implementation for Android, or someone will have to fork it and add it.

darkguy2008 commented 5 years ago

@mlynch well, the plugin won't do it by itself... which is bad. I mean, there's a large amount of GPS-based apps and Ionic is one of the best frameworks to develop mobile apps, so having a empty function for getting the geolocation basically beats the purpose of using it at all. I find it really bad that the "official" plugin is lacking something so important.

Anyways, I just found a fork that had the code implemented: https://github.com/chemerisuk/cordova-plugin-geolocation.git . So far it works pretty well, someone should merge it.

mlynch commented 5 years ago

Well step one is to just have a discussion about it and figure out if the geolocation support in Android webview's is still inadequate (It was in January). Adding it back is another discussion.

As for that fork, do you know anything more about it? How did you find it, how far has it diverged, etc.? It's possible we could recommend that fork if it is of quality.

terminaltor commented 5 years ago

Here is my solution:

You will need use: Platform: https://ionicframework.com/docs/api/platform/Platform/ Diagnostic: https://ionicframework.com/docs/native/diagnostic/ Geolocation: https://ionicframework.com/docs/native/geolocation/ Location Accuracy: https://ionicframework.com/docs/native/location-accuracy/

getUserPosition() {
    const HIGH_ACCURACY = 'high_accuracy'';
    if (this.platform.is('cordova')) {
      this.diagnostic.isLocationEnabled().then(enabled => {
        if (enabled) {
          this.diagnostic.getLocationMode().then(locationMode => {
            if (locationMode === HIGH_ACCURACY) {
              this.geolocation.getCurrentPosition().then((position) => {
                // populate my data
              });
            } else {
                this.askForHighAccuracy();
            }
          });
        } else {
          // handle if user deny location 
        }
      });
    } 
  }

askForHighAccuracy() {
 this.locationAccuracy
   .request(this.locationAccuracy.REQUEST_PRIORITY_HIGH_ACCURACY).then(() => {
     this.geolocation.getCurrentPosition().then((position) => {
       // populate my data
     });
   });
}

After this I need that the user set the High Accuracy mode and accept google terms to use.

Hope it work for you guys =)

Works for me... thanks

Ravenstine commented 5 years ago

@darkguy2008 Sadly, I can't get that person's fork to build. Is there anything specific you did when installing it?

I ran cordova plugin add https://github.com/chemerisuk/cordova-plugin-geolocation.git and when I run cordova build android I get this:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'.
> java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex

That's even after blowing away my platform and starting from scratch.

darkguy2008 commented 5 years ago

@terminaltor I tried that method and it didn't work for me, same empty object and a lot more verbose than it should be needed. I might give it a try again later...

@Ravenstine that's weird, it must be something else within your project, in mine it builds pretty well. I don't have much ionic experience, so I have no idea what could be affecting your build, but the last commit was 20 Jun and it works in my project so it must be something else. I just followed the steps from the Readme, the same command you did and added import { Geolocation } from '@ionic-native/geolocation'; in my code. Geolocation is made like this:

  public async geolocate(maxTries: number = 1) {
    let rv = undefined;
    let locations = [];
    while (locations.length < maxTries) {
      try {
        let result = await this.geolocation.getCurrentPosition({
          enableHighAccuracy: true,
          maximumAge: 0,
          timeout: 60000
        });
        locations.push(result);
      }
      catch (e) {
      }
      await this.delay(1000);
    }
    locations = locations.filter(x => x.coords && x.coords.latitude);
    if (locations.length > 0)
      rv = _.last(locations);
    return rv;
  }