0

I have an app that, when a certain activity starts, needs to fill in one field with the current address. I'm not sure the way I'm doing it ever updates the gps coordinates unless another app, that uses gps, is started.

I can get the address with the Geocoder if I have a location. And I can get the location with the getLastLocation of the Google Locations API.

If I follow the tutorial steps and initialize the api in onCreate like this:

GoogleApiClient.Builder(this).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(LocationServices.API).build();

and in onConnect get the last location like this:

Location lastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

I get the feeling that I'm only getting the last known address but this above code doesn't actually start the GPS on the phone.

Do I need to enable location updates in onConnected and then the first time I get the location I turn of the updates or will the above code actually start the GPS?

Mathias Rönnlund
  • 4,078
  • 7
  • 43
  • 96

1 Answers1

1

getLastLocation method doesn't start GPS, just retrieves last location.

Also you can check the location's date to detect if it's too old.

In the case of old location or want get new one, you need make location request.

Consider my LocationRequest

private LocationRequest mLocationRequest;

private void createLocationRequest(){
    mLocationRequest = new LocationRequest();

    // 0 means here receive location as soon as possible
    mLocationRequest.setInterval(0);
    mLocationRequest.setFastestInterval(0);

    // setNumUpdates(1); stops location requests after receiving 1 location
    mLocationRequest.setNumUpdates(1);

    // PRIORITY_HIGH_ACCURACY option uses your GPS
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}

@Override
public void onLocationChanged(Location location) {
    // Use new location
}
Graham
  • 7,431
  • 18
  • 59
  • 84
blackkara
  • 4,900
  • 4
  • 28
  • 58