0

I want my Kotlin app to get the current accurate (GPS) location once only when requested by the user.

Up to now I've been using fusedLocationClient.lastLocation. This has generally worked successfully, but occasionally the app returns a distant location (sometimes a mile or two away) even when Google Maps and another app running on the same phone have an accurate GPS location.

I've tried implementing the getCurrentLocation method using code from How to get location using "fusedLocationClient.getCurrentLocation" method in Kotlin?. However, Android Studio reports an error at fusedLocationClient.getCurrentLocation(PRIORITY_HIGH_ACCURACY, object : CancellationToken()

saying that I should either:

"Rename reference"

or

"Create extension function for 'FusedLocationProviderClient.getCurrentLocation'"

Is it possible to force the lastLocation method to use the GPS location? (In the Manifest I have only included the ACCESS_FINE_LOCATION permission, but that doesn't seem to do the job.)

Failing that, can anyone show me how to create an extension function for the getCurrentLocation method, please?. I've not found any examples online.


EDIT, following Gabe Sechan's answer

Although the FusedLocation provided by Google Play Services is often said to be what Google prefers, it cannot be relied on to provide the most accurate available location, as Gabe Sechan has commented in his answer.

For my purpose, therefore, I have turned to imthegaga's code provided in his answer in Android Location Manager, Get GPS location ,if no GPS then get to Network Provider location - Stack Overflow. This code uses the Android location classes to get the last known locations from the GPS Provider if available and from the Network Provider if available and, if both are available, returns the more accurate one.

prepbgg
  • 3,564
  • 10
  • 39
  • 51

1 Answers1

1

Ok, a few confued concepts here.

1)Fused location is NOT GPS. It can be, but it may not be. Fused is an attempt to combine GPS (high power drain, slow lock) with Network (low power drain, requires server assistance) to provide a blend of accuracy and power. If you want GPS, you need to use the Android location classes, not the Google Play FusedLocation stuff (bonus is the Android stuff works even if you don't have Google Play on the device).

2)lastLocation returns a cached location. It doesn't update the location, or get the current location. It returns the location of the last time the location functionality was turned on. Which could be very old, or could be null if there is no cached data. It's an optimization, and should only be used if you know what you're doing and don't need up to date data. If you do, you need to use requestLocationUpdates which actually turns on the location subsystem and finds the location.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • Thanks for your helpful response. My confusion probably stems from statements in the Android documentation such as "[location information] ... is available in the Location object that your app can retrieve from the fused location provider. In response, the API updates your app periodically with the best available location, based on the currently-available location providers such as WiFi and GPS". From this (and other online documents) I assumed that the fused location provider was preferable to the old location classes in all circumstances. – prepbgg Jun 22 '23 at 17:38
  • From the Android doc it is clear that the fused location provider CAN use GPS. However, I assume from what you say that there is no way to force it to use GPS. So, I shall go back to using the old location classes for my current purpose. – prepbgg Jun 22 '23 at 17:38
  • If I could find out how to implement an "extension function" might the PRIORITY_HIGH_ACCURACY help achieve more frequent use of GPS? https://developer.android.com/training/location/change-location-settings says "With this setting, the location services are more likely to use GPS" – prepbgg Jun 22 '23 at 17:44
  • 1
    Yes, HIGH_ACCURACY will increase the chance it uses GPS. It just doesn't force it, it can still use other sources. However I would worry less about the source and more about the accuracy of its location and what accuracy you need. – Gabe Sechan Jun 22 '23 at 17:46
  • 1
    ALso, FusedProvider doesn't replace the LocationManager stuff. It has its own set of tradeoffs. The main one being that not all devices support it, as you have to pay Google to legally have Google Play Services on your device (generally this is paid for by the OEM, not the device owner). Especially in the developing world this isn't the case for all devices. The other being that you have less control over whether GPS or other methods are used. – Gabe Sechan Jun 22 '23 at 17:48