I want to get periodic (say every 2 minutes) current location updates for this I'm following official documentation, I wrote this code but its not giving current location updates every two minutes even specified in LocationRequest object that I'm passing in requestLocationUpdates(), here is code:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback,
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{
private FusedLocationProviderClient FusedLocationClient;
private GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
SupportMapFragment map =
getSupportFragmentManager().findFragmentById(R.id.map));
map.getMapAsync(this);
FusedLocationClient LocationServices.getFusedLocationProviderClient(this);
}
@Override
public void onConnected(Bundle bundle) {
FusedLocationClient.getLastLocation()
.addOnSuccessListener(this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if (location != null) {
Log.i("MainActivity ", "" + location.getLongitude())
}
}
});
FusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
FusedLocationClient.requestLocationUpdates(requestLocation(),
new LocationCallback(){
@Override
public void onLocationResult(LocationResult locationResult) {
for (Location location : locationResult.getLocations()) {
Log.i("MainActivity ", "" + location.getLongitude());
//not getting current location updates every 2 minutes
}
};
},null);
}
@Override
public void onConnectionSuspended(int i) {}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {}
this work for me.
This is similar to my other answer here, updated to use the recently introduced FusedLocationProviderClient class.
In order to use a FusedLocationProviderClient in conjunction with a Google Map:
Wait until the Google Map is ready
Request the Location permission at runtime if needed
Request location updates once the permission is granted
Update the Google Map once the user's location is obtained
First ensure that you're using at least version 11 of Google Play Services, as older versions don't have the FusedLocationProviderClient class (newer versions will work as well):
Note that the FusedLocationProviderClient is present in version 11.0.2, but due to bugs in the initial implementation, it's recommended that you only use this class on 11.6.0 and later. From the documentation:
Here is the full Activity class:
The user will be prompted to accept the Location permission:
The location will be updated on app launch, and every two minutes:
Implement Location change listener and you will be able to override onlocation changed...
For make it simple, try with this library https://github.com/mrmans0n/smart-location-lib. This will use the Fused Location Provider.
You just put this code