3

I'm building an app using Ionic/Angular/Firebase.

The app requires a user's geolocation.. I'm using Ionic native's geolocation plugin and I'm stumbling on some issues with speed for Android.

  • On PC it takes less than 500 milliseconds to get the current geolocation.

  • On iOS it takes about 1 second.

  • On Android it takes 6+ seconds, and sometimes it even fails entirely.

Is this an issue with Ionic Native? Is there a special plugin for Android?

Here is my code:

this.geolocation.getCurrentPosition({enableHighAccuracy: false, timeout: 3000, maximumAge: 100000}).then((resp) => {
   this.userLocation.lat = resp.coords.latitude;
   this.userLocation.lng = resp.coords.longitude;
   this.getLocation();
}).catch((error) => {
  this.geolocation.getCurrentPosition({enableHighAccuracy: true, timeout: 3000, maximumAge: 100000}).then((resp) => {
     this.userLocation.lat = resp.coords.latitude;
     this.userLocation.lng = resp.coords.longitude;
     this.getLocation();
  }).catch((error) => {
    this.userLocation.lat = this.userData.location.lat || 0;
    this.userLocation.lng = this.userData.location.lng || 0;
    //if fails, get users last known location.
    this.getLocation();
    console.log('Error getting location', error);
  });
  console.log('Error getting location', error);
});

I can't imagine this code is wrong.. As I said, it works wonderfully on PC and iOS.. Is there something I should do if the user is on android? Is there a different plugin I could use? Any help would be amazing!

Simon
  • 2,498
  • 3
  • 34
  • 77

4 Answers4

3

The code you are using is working.

What @Maheshvirus is talking about is the 'cordova-plugin-request-location-accuracy' plugin which actually allows you to prompt the user to turn on high location accuracy (and to check and see if it is on first) for GPS, which will 'Use GPS, WiFi, Bluetooth, or cellular networks to determine location'. I use the same plugin in my apps, it works well.

From doing some quick Googling it seems that IOS does not have a high accuracy mode, because by default IOS uses gps, wifi, bluetooth etc to get your location...you cant turn it on and off like Android, which explains why IOS is always so quick to know where you are. So on Android devices you need to prompt the user to turn it on, with the plugin mentioned by @Maheshvirus. Which it then should always stay on unless the user changes their mode to a battery-saving or device-only location mode.

PC should have way faster internet and shouldn't have data caps or severe throttling (unless net neutrality is now completely gone or you're in some other terrible place) so your browser should be able to locate you pretty easily and quickly through the modem/router you're connected to - see this post answer for more details.

The Solution

I'm too tired right now to get you the working code example you need, but utilizing Geolocation.watchPosition() rather than getCurrentPosition() will allow you to get the location much faster in my experience using it - and this user seems to have tested it. The watchPosition 'is used to register a handler function that will be called automatically each time the position of the device changes'. The watchPosition function is also utilized almost exactly the same as getCurrentPosition, except when it is called you have to assign it an ID and use Geolocation.clearwatch(idname) when done. Here is the docs for watchPosition(). So the cool thing is if the user moves or the phones location service thinks they've moved, it won't have to spin up another call to the device GPS (which is what is making it take so long) since the GPS is already listening.

Sorry I didn't offer an actual code fix, but I hope these details help. This was an issue I also ran into when I was utilizing getCurrentPosition on the different devices. You should have no problem implementing watchPosition(), and should see android location latency improvements immediately - especially if you get the user to turn on High Accuracy Location mode with that plugin 'cordova-plugin-request-location-accuracy'

Devin Carpenter
  • 907
  • 8
  • 21
  • This was exactly what I was looking for. Thanks! I awarded you the bounty :) – Simon Jul 10 '18 at 14:04
  • @Jane Doe glad I could help! – Devin Carpenter Jul 10 '18 at 14:45
  • watchPosition triggers only if the App is in foreground in my case don't know something must have went wrong.. – Naga Nov 03 '18 at 13:02
  • As I understood this plugin only for foreground, for background there is other one cordova-plugin-mauron85-background-geolocation – Naga Nov 03 '18 at 13:12
  • @Naga I am not sure which plugin is the best to use to watch for the location in the background. That plugin may be promising but I've never used it, and it was just published yesterday. watchPosition() is a Web API for use in Web Apps and won't work in the background as it seems you [and others have found.](https://stackoverflow.com/questions/33926217/it-is-possible-to-watch-the-location-in-the-background-on-mobile-ios-android) – Devin Carpenter Nov 04 '18 at 22:21
1

There is nothing wrong with the code. I was also facing the same issue in android phones. Solution i have tried.

Manual Solution

  • Going into the location setting option. Check whether the location mode is not set to Device Only. Set it to High Accuracy and then try
  • OFF And ON your location setting option going into setting-> location option. Restart Your Phone and try

  • Check for google map whether it's fetching your current location or not. Try to fetch your current location on google map. Anyway, If google map won't find your current location then you won't get location access into your app.

Once you find the current location on google map you probably get the location into your app.

Solution using plugin

<plugin name="cordova-plugin-request-location-accuracy" spec="*" /> 
Maheshvirus
  • 6,749
  • 2
  • 38
  • 40
  • Hi, thanks for the comment. Regarding the manual approach, Google Maps loads instantly with my current location, and when I open my app after 6 seconds it times out and can't find my location. Regarding the plugin, what is that supposed to do? Is it a plugin I am supposed to install? – Simon Jul 09 '18 at 14:06
  • please check you have given location access to your installed app or not.if you selected denie access for location then app will not fetch your location. from sdk version 23 and above it will ask you for allow location access while you trying to fetch location first time.at that time you have to select allow location.if you have android sdk version less than 22 it will not ask you for that.its automatically take location access permissions. – Maheshvirus Jul 10 '18 at 04:52
  • about cordova-plugin-request-location-accuracy plugin it will ask you everytime for allow gps access popoup when your gps is not enabled or not able to fetch due to some reasons.it will fix your problem.in naugat this permission issue is causing more. – Maheshvirus Jul 10 '18 at 04:55
  • first fetch google current location. if u r finding it then ..some times it requirs to restart your phone .still not working reinstall your app – Maheshvirus Jul 10 '18 at 04:57
0

I had a similar situation with Geolocation. Its vital that you only call the native methods after the Platform is ready. While this is how you're expected to call it, overlooking it will result in significant delay before you get a response from the plugin, more so in Android.

import { Platform } from 'ionic-angular'
...

constructor( private platform: Platform) { 
this.platform.ready().then(() => {
  //request location here
 })
}
...
fitzmode
  • 1,007
  • 1
  • 18
  • 29
0

I just remove maximumAge in options. My finaly options:

 { enableHighAccuracy: true, timeout: 10000 }
Sajad Speed
  • 403
  • 2
  • 14