Calculates the distance between 2 coordinates (latitude and longitude),. Net

Source: Internet
Author: User
Tags cos sin

. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.