0

I made changes in my location based android app to conform to latest API. So now my activity implements:

public class MapActivity extends FragmentActivity implements   LocationListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
.....
}

In onCreate, I configure LocationRequest and GoogleApiClient:

mLocationRequest = LocationRequest.create()
            .setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY)
            .setInterval(2 * 1000);
mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();

And here is my onConnected method:

@Override
public void onConnected(Bundle bundle) {
     Log.i(TAG, "Location services connected.");
     Location location =  LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);        
     if (location == null) {               LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,  mLocationRequest, this);
     } else {
        .....
        .....
    }
}

Issue I am facing is that onLocationChanged is not getting called even when user is on move.

What am I missing here?

During debugging I found that in onConnected():

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

The location is not null, hence code inside if is not executed. Can that be the reason for onLocationChanged not getting called?

Mandroid
  • 6,200
  • 12
  • 64
  • 134
  • Are you sure localization services are turned on on your smartphone? – greywolf82 Feb 08 '15 at 09:21
  • That I am not sure....how do check that.BTW, I have googleMap.setMyLocationEnabled(true); So when map activity is running, user location is constantly updated on map. So I guess location services is working fine. – Mandroid Feb 08 '15 at 14:11
  • I just checked....location services are enabled on my device. – Mandroid Feb 08 '15 at 14:17
  • LocationRquest is as: mLocationRequest = LocationRequest.create() .setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY) .setInterval(5000) .setSmallestDisplacement(25.0f); Moved code inside if out. So now onLocationChanged called,but only once.I assume that it must be called even if position is unchanged as I have set 5 secs in setInterval. – Mandroid Feb 08 '15 at 14:58
  • 1
    It was related to priority level.I set it to PRIORITY_HIGH_ACCURACY, and now calls are frequent.I expected that with PRIORITY_BALANCED_POWER_ACCURACY I would get real time location updates.But this was not the case. – Mandroid Feb 08 '15 at 16:14

0 Answers0