How to Get Distance between Two Points in Android
Takahiro Iwasa
1 min read
Android GIS Java
It has been more than 3 years since this post was published.
Distance between two points can be calculated using android.location.Location
in Android. Happy Coding!
/**
* Get distance in meters between two points.
* @param latitude1
* @param longitude1
* @param latitude2
* @param longitude2
* @return Distance between two points
*/
public float getDistanceBetween(
double latitude1, double longitude1,
double latitude2, double longitude2) {
float[] results = new float[3];
// Get distance between two points (Note that it will be set to the fifth argument)
Location.distanceBetween(latitude1, longitude1, latitude2, longitude2, results);
if (results.length < 1) {
// You can handle failure here.
}
if (results.length < 3) {
// You can handle azimuth here.
}
// Return only distance in meters
return results[0];
}