Rakesh Burbure
Rakesh Burbure

Reputation: 1045

Android - How I can find the distance between two points - Java code?

I have lat and lan of two points. How i can find distance between these two points ? If you have any code or link, then please suggest me.

Thank You.

Upvotes: 0

Views: 281

Answers (2)

Leonidos
Leonidos

Reputation: 10518

You can use Android SDK function distanceBetween.

Computes the approximate distance in meters between two locations, and optionally the initial and final bearings of the shortest path between them. Distance and bearing are defined using the WGS84 ellipsoid.

Upvotes: 2

MuraliGanesan
MuraliGanesan

Reputation: 3259

Try this, You will get distance

 private String distance(double lati, double longi, double curlati1,
        double curlongi1) {

    double theta = longi - curlongi1;
    double dist = Math.sin(deg2rad(lati)) * Math.sin(deg2rad(curlati1))
            + Math.cos(deg2rad(lati)) * Math.cos(deg2rad(curlati1))
            * Math.cos(deg2rad(theta));
    dist = Math.acos(dist);
    dist = rad2deg(dist);
    dist = dist * 60 * 1.1515;

    dist = dist * 1.609344;

    result = Double.toString(dist);
    System.out.println("dist_result :" + result);
    return (result);
}

/* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */
/* :: This function converts decimal degrees to radians : */
/* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */
private double deg2rad(double deg) {
    return (deg * Math.PI / 180.0);
}

/* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */
/* :: This function converts radians to decimal degrees : */
/* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */
private double rad2deg(double rad) {
    return (rad * 180.0 / Math.PI);
}

Upvotes: 1

Related Questions