How to Get Distance between Two Points in Android

How to Get Distance between Two Points in Android

Takahiro Iwasa
(岩佐 孝浩)
Takahiro Iwasa (岩佐 孝浩)
1 min read
Android GIS Java

Distance between two points can be calculated using android.location.Location in Android.

/**
 * 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];
}
Takahiro Iwasa
(岩佐 孝浩)

Takahiro Iwasa (岩佐 孝浩)

Software Developer at KAKEHASHI Inc.
Involved in the design, development, and operation of the prescription data collection platform