Android で2地点間の距離を取得する方法
岩佐 孝浩
1 min read
Android GIS Java
この記事は、公開後3年以上が経過しています。
android.location.Location
を使用して、2地点間の距離を取得できます。
/**
* 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];
}