Reputation: 45
I am having trouble figuring how to get the distance between 2 non fixed locations. The first location is my current location and the second is the latest current location after walking say 100 meters. Below is a snippet of code.
/**
* Callback that fires when the location changes.
*/
@Override
public void onLocationChanged(Location location) {
mCurrentLocation = location;
updateUI();
}
/**
* Updates the latitude, the longitude, and the last location time in the UI.
*/
private void updateUI() {
latitude = mCurrentLocation.getLatitude();
longitude = mCurrentLocation.getLongitude();
mLatitudeTextView.setText(String.format("%s: %f", mLatitudeLabel,latitude));
mLongitudeTextView.setText(String.format("%s: %f", mLongitudeLabel, longitude));
}
So below I have my mCurrentLocation but how do I get the newCurrentLocation so I use the distanceTo method.
double distance = mCurrentLocation.distanceTo(newCurrentLocation);
Any suggestions would be much appreciated.
Upvotes: 0
Views: 718
Reputation: 6151
You'll have to save the current location in a temporary variable, something like this:
public void onLocationChanged(Location location) {
Location temp = mCurrentLocation; //save the old location
mCurrentLocation = location; //get the new location
distance = mCurrentLocation.distanceTo(temp); //find the distance
updateUI();
}
Upvotes: 1