//get distance based on latitude and longitude public static double GetDistance_m(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)); if (dist > 1) dist = 1; if (dist < -1) dist = -1; dist = Math.Acos(dist); dist = Rad2Deg(dist); dist = dist * 60 * 1.1515; dist = dist * 1609.344; return (dist); } //get distance based on latitude and longitude public static double GetDistance_km(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)); if (dist > 1) dist = 1; if (dist < -1) dist = -1; dist = Math.Acos(dist); dist = Rad2Deg(dist); dist = dist * 60 * 1.1515; dist = dist * 1609.344 / 1000; return (dist); } //convert degree to radian public static double Deg2Rad(double deg) { return (deg * Math.PI / 180.0); } //convert radian to degree public static double Rad2Deg(double rad) { return (rad / Math.PI * 180.0); }
Sunday, July 21, 2013
Calculate distance between two geolocations
The following methods can be used to calculate the actual distance between two geo locations in (lat, lon)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment