5

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!!

Jack
  • 115
  • 1
  • 2
  • 8

5 Answers5

11

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);
Scott Helme
  • 4,786
  • 2
  • 23
  • 35
  • but thats for calculating from one point to another without directly right... what about the buildings, obstacles and etc.? – Jack Jul 22 '13 at 12:21
  • hmm.. is there anyway I can implement that function as well? or its not possible... – Jack Jul 22 '13 at 12:23
  • 1
    It's possible, no doubt about that, but it's not as easy as the code above. That's the distance as the crow flies. – Scott Helme Jul 22 '13 at 12:24
  • The Google Directions API is what you need: https://developers.google.com/maps/documentation/directions/ – Scott Helme Jul 22 '13 at 12:25
  • ah well.. something is better then nothing haha thanks for the help :) i'll give it a try – Jack Jul 22 '13 at 12:31
4

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!

Frackinfrell
  • 327
  • 2
  • 12
AitorTheRed
  • 553
  • 4
  • 15
1

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;

}
ibesora
  • 197
  • 5
0

Use the below method for calculating the distance of two different locations.

 public double getKilometers(double lat1, double long1, double lat2, double long2) {
    double PI_RAD = Math.PI / 180.0;
    double phi1 = lat1 * PI_RAD;
    double phi2 = lat2 * PI_RAD;
    double lam1 = long1 * PI_RAD;
    double lam2 = long2 * PI_RAD;

    return 6371.01 * acos(sin(phi1) * sin(phi2) + cos(phi1) * cos(phi2) * cos(lam2 - lam1));
}
Daxesh V
  • 571
  • 6
  • 12
0
try this

/**
 * This is the implementation Haversine Distance Algorithm between two places
 * @author ananth
 * R = earth’s radius (mean radius = 6,371km)
 Δlat = lat2− lat1
 Δlong = long2− long1
 a = sin²(Δlat/2) + cos(lat1).cos(lat2).sin²(Δlong/2)
 c = 2.atan2(√a, √(1−a))
 d = R.c
 *
 */

public class HaversineDistance {

/**
 * @param args
 * arg 1- latitude 1
 * arg 2 — latitude 2
 * arg 3 — longitude 1
 * arg 4 — longitude 2
 */
 public static void main(String[] args) {
 // TODO Auto-generated method stub
 final int R = 6371; // Radious of the earth
 Double lat1 = Double.parseDouble(args[0]);
 Double lon1 = Double.parseDouble(args[1]);
 Double lat2 = Double.parseDouble(args[2]);
 Double lon2 = Double.parseDouble(args[3]);
 Double latDistance = toRad(lat2-lat1);
 Double lonDistance = toRad(lon2-lon1);
 Double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2) + 
 Math.cos(toRad(lat1)) * Math.cos(toRad(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;

 System.out.println(“The distance between two lat and long is::” + distance);

}

 private static Double toRad(Double value) {
 return value * Math.PI / 180;
 }

}
Jose alves
  • 31
  • 4