. Net calculates the distance between 2 points, or calculates the distance between 2 coordinates and 2 latitude and longitude.
The code is as follows:
1 /// <summary>2 ///calculates the distance between the Geographics locations3 /// </summary>4 Public Static classgeodistance5 {6 Public Static DoubleBetween (DoubleLatitude1,DoubleLongitude1,DoubleLatitude2,DoubleLongitude2,7Distanceunits units =distanceunits.kilometers)8 {9 DoubleA1 = Latitude1 *Radianconversionfactor;Ten DoubleB1 = Longitude1 *Radianconversionfactor; One Doublea2 = Latitude2 *Radianconversionfactor; A Doubleb2 = Longitude2 *Radianconversionfactor; - - DoubleDistance = theMath.acos (Math.Cos (A1) * MATH.COS (B1) * MATH.COS (A2) * Math.Cos (B2) + -Math.Cos (A1) * Math.sin (B1) * MATH.COS (A2) * Math.sin (B2) + math.sin (A1) * Math.sin (A2)) *Distanceconstants[units]; - - returndistance; + } - + Private Const DoubleRadianconversionfactor = Math.PI/ the; A at Private Static ReadOnlyDictionary<distanceunits,Double> distanceconstants =NewDictionary<distanceunits,Double> (3) { -{distanceunits.miles,3963.1 }, -{distanceunits.nauticalmiles,3443.9 }, -{distanceunits.kilometers,6378 } - }; - } in - Public enumdistanceunits to { + Miles, - Nauticalmiles, the kilometers, *}
The code was simple, but it took a lot of effort. is not yet tested how efficient. If there is an efficient algorithm, please do not stingy advice!!!
Calculates the distance between 2 coordinates (latitude and longitude),. Net