Wednesday 23 July 2014

Calculate Distance by Latitude and Longitude using C#

Sample Code (C#)

01using System;
02//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
03//:::                                                                         :::
04//:::  This routine calculates the distance between two points (given the     :::
05//:::  latitude/longitude of those points). It is being used to calculate     :::
06//:::  the distance between two locations using GeoDataSource(TM) products    :::
07//:::                                                                         :::
08//:::  Definitions:                                                           :::
09//:::    South latitudes are negative, east longitudes are positive           :::
10//:::                                                                         :::
11//:::  Passed to function:                                                    :::
12//:::    lat1, lon1 = Latitude and Longitude of point 1 (in decimal degrees)  :::
13//:::    lat2, lon2 = Latitude and Longitude of point 2 (in decimal degrees)  :::
14//:::    unit = the unit you desire for results                               :::
15//:::           where: 'M' is statute miles                                   :::
16//:::                  'K' is kilometers (default)                            :::
17//:::                  'N' is nautical miles                                  :::
18//:::                                                                         :::
19//:::  Worldwide cities and other features databases with latitude longitude  :::
21//:::                                                                         :::
22//:::  For enquiries, please contact sales@geodatasource.com                  :::
23//:::                                                                         :::
25//:::                                                                         :::
26//:::           GeoDataSource.com (C) All Rights Reserved 2014                :::
27//:::                                                                         :::
28//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
29 
30private double distance(double lat1, double lon1, double lat2, double lon2, char unit) {
31  double theta = lon1 - lon2;
32  double dist = Math.Sin(deg2rad(lat1)) * Math.Sin(deg2rad(lat2)) + Math.Cos(deg2rad(lat1)) * Math.Cos(deg2rad(lat2)) * Math.Cos(deg2rad(theta));
33  dist = Math.Acos(dist);
34  dist = rad2deg(dist);
35  dist = dist * 60 * 1.1515;
36  if (unit == 'K') {
37    dist = dist * 1.609344;
38  } else if (unit == 'N') {
39    dist = dist * 0.8684;
40    }
41  return (dist);
42}
43 
44//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
45//::  This function converts decimal degrees to radians             :::
46//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
47private double deg2rad(double deg) {
48  return (deg * Math.PI / 180.0);
49}
50 
51//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
52//::  This function converts radians to decimal degrees             :::
53//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
54private double rad2deg(double rad) {
55  return (rad / Math.PI * 180.0);
56}
57 
58Console.WriteLine(distance(32.9697, -96.80322, 29.46786, -98.53506, "M"));
59Console.WriteLine(distance(32.9697, -96.80322, 29.46786, -98.53506, "K"));
60Console.WriteLine(distance(32.9697, -96.80322, 29.46786, -98.53506, "N"));

No comments:

Post a Comment