-1

I'm building an app that requires your current location to initialize and show a map. I am requesting location updates with LocationManager.requestLocationUpdates(...), but this only appears to give me data when the location changes (for example I move 10 feet). I am also attempting to use getLastKnownLocation, but in some uncommon cases (like turning the GPS on and without moving opening the app) this will return null for all providers I am making use of.

Whenever getLastKnownLocation returns null, my app can't initialize until I move 10 feet and receive a location update. So far I haven't found a method to poll for my location right now, nor have I found anything suggesting that when I requestLocationUpdates(...) I can get my first data the moment I subscribe and not when I either wait the specified time period or move the specified distance.

One approach would be to requestLocationUpdates(...) on a very small or 0 time interval, get that to send me my current location, delete the LocationListener and again requestLocationUpdates(...) but this time with the much larger value I use to avoid drainig the battery.

I'm looking for a better way to solve this. Probably something like getCurrentLocation(), but really anything that works.

Blueriver
  • 3,212
  • 3
  • 16
  • 33
  • 2
    to get an accurate reading any solution is going to involve waiting for a location. if the gps provider cant get a location you will get null. the FusedLocation API has something where you can set the number of updates you want so you can just get one if thats all you need but you still have to wait https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest.html#public-methods – tyczj May 05 '16 at 16:54
  • 1
    "Probably something like getCurrentLocation()" -- that does not exist, because you are not guaranteed to be able to get a location. Beyond the standard issue of users disabling providers or blocking the permissions on Android 6.0+, the device may be incapable of getting a location due to environmental factors. The device may be in an underground parking garage, for example. Could there be a `getCurrentLocation()` with some sort of timeout value? Yes, but that's not especially difficult for you to implement yourself. – CommonsWare May 05 '16 at 16:58

2 Answers2

3

I am requesting location updates with LocationManager.requestLocationUpdates(...), but this only appears to give me data when the location changes (for example I move 10 feet).

It will give you the first location update as soon as the location is determined -- whether you are moving or not. And it wouldn't be a bad idea to keep listening for more updates for a while as the first one(s) could be less accurate.

I'm looking for a better way to solve this. Probably something like getCurrentLocation(), but really anything that works.

If you don't want to receive continuous updates, then what about one of the LocationManager.requestSingleUpdate() variants?

Those will give you just one location update in the onLocationChanged() callback (or via an Intent) and that's it. No need to cancel any listeners etc. The call could be something like:

locationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, this, Looper.getMainLooper());
Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
  • Apparently I missed both the fact that requestLocationUpdates gives me an update as soon as the location is determined. As far as I understood it (and tested it, though I didn't test it very far) I had to actually move the min distance or wait the specified time interval to get the first update. Thanks a lot for these clarifications. – Blueriver May 05 '16 at 21:58
2

well i think you using only onLocationChanged method to do your work.

do like this.

@Override
public void onConnected(@Nullable Bundle bundle) {
  updateUi();   
  }` 

public void updateUi() {
        try {
            mlastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
            if (mlastLocation != null) {
                final double latitude = mlastLocation.getLatitude();
                final double longitude = mlastLocation.getLongitude();

               //using this cordinates you can initialize map

                }
        } catch (SecurityException e) {
            e.printStackTrace();
        }
    }`
Suraj Kumar
  • 95
  • 1
  • 9