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. 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];
}
Takahiro Iwasa

Takahiro Iwasa

Software Developer at KAKEHASHI Inc.
Involved in the requirements definition, design, and development of cloud-native applications using AWS. Now, building a new prescription data collection platform at KAKEHASHI Inc. Japan AWS Top Engineers 2020-2023.