Given the latitude and longitude of 2 points, we can find the distance between then using the Haversine formula.
The Java function to do the same is given below :
public static double getDistance(double lat1, double lon1, double lat2, double lon2)
{
int R = 6371; // km
double dLat = Math.toRadians(lat2-lat1);
double dLon = Math.toRadians(lon2-lon1);
double lat1mod = Math.toRadians(lat1);
double lat2mod = Math.toRadians(lat2);
double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1mod) * Math.cos(lat2mod);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double d = R * c;
return d;
}
No comments:
Post a Comment