0

I am developing an app that needs current lat and long of the user at start up. Currently i am getting lat and long of an user like this:

@Override
protected void onCreate(Bundle savedInstanceState) 
{

super.onCreate(savedInstanceState);

GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
}

public void onConnected(Bundle connectionHint) {

LocationRequest mLocationRequest = LocationRequest.create(); 
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
mLocationRequest.setInterval(1000); // Update location every second
mLocationRequest.setNumUpdates(1);

LocationServices.FusedLocationApi.requestLocationUpdates(
                        mGoogleApiClient, mLocationRequest, this);

}

Different listeners that are needed for location requests are implemented in Activity.

Sometimes the request takes 1-2 seconds then other times 15 seconds and sometimes even more. My question is: am i doing anything wrong that i am not seeing?

Using GetLastKnownLocation() is not an option because i need current location.

Any tips would be greatly appreciated.

linkovic
  • 21
  • 4
  • Try to get location using network operator, because GPS may take long time to return location. – Silvans Solanki Apr 07 '15 at 13:01
  • @SilvansSolanki Forgive my ignorance but isnt' FusedLocationApi doing excatly that? Can you give me any snippet/tutorial? – linkovic Apr 07 '15 at 13:06
  • What you are doing is ok. Try to set number updates from 3 to 5 instead of 1, because generally the first location is not really accurate. In our app we needed something similar and finally we decided to check the location five times in certain activities of the app and then saving that location into shared preferences in order to consult them when we need. Is a little bit tricky but at least we always have location updated – Joaquin Iurchuk Apr 07 '15 at 13:15

0 Answers0