I want to calculate the distance between my location and another location and write it in a TextView at intervals of time.
I tried this:
private LocationListener mLocationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Log.d(TAG_LOG, "In onLocationChanged(...)");
if(!mLatLngGpxList.isEmpty()) {
Location loc = new Location("");
loc.setLatitude(mLatLngGpxList.get(0).getLatitude());
loc.setLongitude(mLatLngGpxList.get(0).getLongitude());
float distance = mCurrentLocation.distanceTo(loc)/1000;
distanceTextView.setText("Distance to Track: " + distance + " Km");
}
}
};
(I have this too..)
final LocationRequest locationRequest = new LocationRequest();
locationRequest.setInterval(UPDATE_INTERVAL);
locationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL);
locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
mLocationClient.requestLocationUpdates(locationRequest, mLocationListener);
The distance chages only when the application is restarted and not in real time.
Maybe i should use a handler?