Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a method using Haversine Formula, Android V2

I'm not really good with mathematics but I need to calculate the distance of two different locations of the markers. Something like this:

public double CalculationByDistance(double initialLat, double initialLong, double finalLat, double finalLong){

return distance;
}

Or is there any alternative ways that I can calculate the distance of two markers, also I tried to google for answers.. but couldn't find any.

Reference: http://en.wikipedia.org/wiki/Haversine_formula

Comments are appreciated :) Thanks!!

like image 783
Jack Avatar asked Jul 22 '13 12:07

Jack


People also ask

How does the haversine function work?

The haversine formula determines the great-circle distance between two points on a sphere given their longitudes and latitudes. Important in navigation, it is a special case of a more general formula in spherical trigonometry, the law of haversines, that relates the sides and angles of spherical triangles.

Does Google Maps use haversine?

Beside the uses of Google Maps API 3, the system is used haversine algorithm to calculated and analyse the distance between two coordinates (Essayad, 2011).


3 Answers

Try this, much simpler than Haversine!

Location me   = new Location("");
Location dest = new Location("");

me.setLatitude(myLat);
me.setLongitude(myLong);

dest.setLatitude(destLat);
dest.setLongitude(destLong);

float dist = me.distanceTo(dest);
like image 163
Scott Helme Avatar answered Nov 03 '22 00:11

Scott Helme


If you want to stick with Haversine, something like this:

public double CalculationByDistance(double initialLat, double initialLong,
                                double finalLat, double finalLong){
    int R = 6371; // km (Earth radius)
    double dLat = toRadians(finalLat-initialLat);
    double dLon = toRadians(finalLong-initialLong);
    initialLat = toRadians(initialLat);
    finalLat = toRadians(finalLat);

    double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
            Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(initialLat) * Math.cos(finalLat); 
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
    return R * c;
}

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

Also, you need to create a method toRadians() that convert values from degrees to radians, which is quite easy. Hope it helps!

like image 31
AitorTheRed Avatar answered Nov 02 '22 23:11

AitorTheRed


From your wikipedia link, applying the formula directly you can do the following:

public double CalculationByDistance(double initialLat, double initialLong, double finalLat, double finalLong){
    /*PRE: All the input values are in radians!*/

    double latDiff = finalLat - initialLat;
    double longDiff = finalLong - initialLong;
    double earthRadius = 6371; //In Km if you want the distance in km

    double distance = 2*earthRadius*Math.asin(Math.sqrt(Math.pow(Math.sin(latDiff/2.0),2)+Math.cos(initialLat)*Math.cos(finalLat)*Math.pow(Math.sin(longDiff/2),2)));

    return distance;

}
like image 20
ibesora Avatar answered Nov 03 '22 00:11

ibesora