Fused Location provider should call onLocationChanged () only when the user's location changes
My application only requires determining the user's location when the user's location changes. I can find out the user's location very easily when wifi / gps is on and when I code this:
mLocationRequest.setInterval(LocationUtils.UPDATE_INTERVAL_IN_MILLISECONDS); //10 seconds
mLocationRequest.setFastestInterval(LocationUtils.FAST_INTERVAL_CEILING_IN_MILLISECONDS);
By coding this, onLocationChanged () is called every 10 seconds, even if the user's location does not change. What for? This will not discharge the battery properly. I want that when the user moves, only my app should have an updated location. Google I / O 2013 says that with sensors (Fused Location Provider), the user's location does not need to be tracked if they are sitting in one place. I commented out the above lines and coded:
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
mLocationRequest.setSmallestDisplacement(50.0f); //50 meters
I only see once that I was walking 50 meters and onLocationChanged () is called. I have never seen onLocationChanged () called at other times.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get handles to the UI view objects
// Create a new global location parameters object
mLocationRequest = LocationRequest.create();
/**
* If below two lines are commented the location change method is not called.
*/
// mLocationRequest.setInterval(LocationUtils.UPDATE_INTERVAL_IN_MILLISECONDS); //Set the update interval
//mLocationRequest.setFastestInterval(LocationUtils.FAST_INTERVAL_CEILING_IN_MILLISECONDS); // Set the interval ceiling to one minute
// Use high accuracy
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
mLocationRequest.setSmallestDisplacement(50.0f);
/*
* Create a new location client, using the enclosing class to
* handle callbacks.
*/
mLocationClient = new LocationClient(this, this, this);
}
@Override
public void onStart() {
super.onStart();
mLocationClient.connect();
}
}
public void getLocation(View v) {
// If Google Play Services is available
if (servicesConnected()) {
// Get the current location
Location currentLocation = mLocationClient.getLastLocation();
// Display the current location in the UI
mLatLng.append("\nLast Location: "+LocationUtils.getLatLng(this, currentLocation));
}
}
public void startUpdates(View v) {
mUpdatesRequested = true;
if (servicesConnected()) {
startPeriodicUpdates();
}
}
@Override
public void onConnected(Bundle bundle) {
mConnectionStatus.setText(R.string.connected);
if (mUpdatesRequested) {
startPeriodicUpdates();
}
}
@Override public void onConnected (Bundle bundles) {mConnectionStatus.setText (R.string.connected);
if (mUpdatesRequested) {
startPeriodicUpdates();
}
}
public void onLocationChanged (Location location) {
// Report to the UI that the location was updated
mConnectionStatus.setText(R.string.location_updated);
// In the UI, set the latitude and longitude to the value received
mLatLng.append("\nLoc Changed:"+LocationUtils.getLatLng(this, location));
}
/**
* In response to a request to start updates, send a request
* to Location Services
*/
private void startPeriodicUpdates() {
mLocationClient.requestLocationUpdates(mLocationRequest, this);
mConnectionState.setText(R.string.location_requested);
}
How can I call onLocationChanged () only when the user's location changes. If the user is sitting in one place, this method should not be called.
source to share
No one has answered this question yet
Check out similar questions: