3

I have two locations and I want to calculate distance in meter. I wrote some code but it's not working perfectly.

private void getDistanceBetweenTwoPoints(double lat1,double lon1,double lat2,double lon2)
{
    Location loc1 = new Location("");
    loc1.setLatitude(lat1);
    loc1.setLongitude(lon1);

    Location loc2 = new Location("");
    loc2.setLatitude(lat2);
    loc2.setLongitude(lon2);

    int R = 6371; // km

    double dLat = deg2rad(lat2-lat1);
    double dLon = deg2rad(lon2-lon1);
    double  a =
            Math.sin(dLat/2) * Math.sin(dLat/2) +
                    Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
                            Math.sin(dLon/2) * Math.sin(dLon/2)
            ;
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
    double distanceInMeters = R * c;
    Log.e("distanceInMeters",distanceInMeters/10000+"mm");
}

public double  deg2rad(double deg) {
    return deg * (Math.PI/180);
}

How can I calculate distance in meter? My goal is, if meter > 200 do something. How can i solve my problem?

Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
BekaKK
  • 2,173
  • 6
  • 42
  • 80

3 Answers3

9

There is no need to re-invent the wheel for this.

You can just use the Location.distanceBetween() method.

From the documentation:

Computes the approximate distance in meters between two locations

Here is a simple example:

private float getDistanceBetweenTwoPoints(double lat1,double lon1,double lat2,double lon2) {

    float[] distance = new float[2];

    Location.distanceBetween( lat1, lon1,
            lat2, lon2, distance);

    return distance[0];
}

If the result array has more than just index zero populated, the other indices each contain a bearing (initial and final).

A bearing is a number that specifies a compass direction.

From http://www.geomidpoint.com/destination/help.html :

The bearing (or azimuth) is the compass direction to travel from the starting point, and must be within the range 0 to 360. 0 represents north, 90 is east, 180 is south and 270 is west.

Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
  • it worked for me. please explain me what is distance[1]? – Vardhman Patil Mar 18 '18 at 18:07
  • @ANDY_VAR You can see the explanation in the link to the documentation: `The computed distance is stored in results[0]. If results has length 2 or greater, the initial bearing is stored in results[1]. If results has length 3 or greater, the final bearing is stored in results[2].` – Daniel Nugent Mar 19 '18 at 14:28
  • Thanks for ur reply. But I would like to know it's practical significance. What is the situation where we get two distances. Also I would like to know what is bearing in this case – Vardhman Patil Mar 20 '18 at 15:18
  • 1
    @ANDY_VAR I just updated the answer with extra info. More info here as wel,l specifically about initial and final bearing: https://www.reddit.com/r/answers/comments/gyfba/can_somebody_explain_to_me_the_difference_between/ – Daniel Nugent Mar 20 '18 at 21:37
  • Please correct the typo for the method name. the correct one is getDistanceBetweenTwoPoints – Emad Razavi Aug 08 '19 at 14:40
0

Use this method to calculate the distance between two lat/long points & their elevation.

/**
 * Calculate distance between two points in latitude and longitude taking
 * into account height difference using the Haversine method as its base.
 *
 * Elevation should be in meters. If you are not interested in elevation, pass 0.
 *
 * @return Distance in meters
 */
private double distanceBetween(double lat1, double lat2, double lon1,
                               double lon2, double el1, double el2) {

    final int R = 6371; // Radius of the earth

    double latDistance = Math.toRadians(lat2 - lat1);
    double lonDistance = Math.toRadians(lon2 - lon1);
    double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2)
            + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2))
            * Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    double distance = R * c * 1000; // convert to meters

    double height = el1 - el2;

    distance = Math.pow(distance, 2) + Math.pow(height, 2);

    return Math.sqrt(distance);
}
Charlie
  • 2,876
  • 19
  • 26
0

You can use below code for calculating distance between two Location in Metres:

private double distanceBetween(double lat1, double lon1, double lat2, double lon2) {
    double theta = lon1 - lon2;
    double dist = Math.sin(deg2rad(lat1))
            * Math.sin(deg2rad(lat2))
            + Math.cos(deg2rad(lat1))
            * Math.cos(deg2rad(lat2))
            * Math.cos(deg2rad(theta));
    dist = Math.acos(dist);
    dist = dist * 180.0 / Math.PI;
    dist = dist * 60 * 1.1515*1000;
    return (dist);
}

private double deg2rad(double deg) {
    return (deg * Math.PI / 180.0);
}
Asif Patel
  • 1,744
  • 1
  • 20
  • 27