1: using System;
2: //::: Passed to function: :::
3: //::: lat1, lon1 = Latitude and Longitude of point 1 (in decimal degrees) :::
4: //::: lat2, lon2 = Latitude and Longitude of point 2 (in decimal degrees) :::
5: //::: unit = the unit you desire for results :::
6: //::: where: 'M' is statute miles :::
7: //::: 'K' is kilometers (default) :::
8: //::: 'N' is nautical miles :::
9: //::: :::
10: //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
11: private double distance(double lat1, double lon1, double lat2, double lon2, char unit) {
12: double theta = lon1 - lon2;
13: double dist = Math.Sin(deg2rad(lat1)) * Math.Sin(deg2rad(lat2)) + Math.Cos(deg2rad(lat1)) * Math.Cos(deg2rad(lat2)) * Math.Cos(deg2rad(theta));
14: dist = Math.Acos(dist);
15: dist = rad2deg(dist);
16: dist = dist * 60 * 1.1515;
17: if (unit == 'K') {
18: dist = dist * 1.609344;
19: } else if (unit == 'N') {
20: dist = dist * 0.8684;
21: }
22: return (dist);
23: }
24:
25: //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
26: //:: This function converts decimal degrees to radians :::
27: //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
28: private double deg2rad(double deg) {
29: return (deg * Math.PI / 180.0);
30: }
31:
32: //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
33: //:: This function converts radians to decimal degrees :::
34: //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
35: private double rad2deg(double rad) {
36: return (rad / Math.PI * 180.0);
37: }