According to the latitude and longitude of two points to achieve the distance between the source code (C #)-Very accurate

Source: Internet
Author: User
Tags cos pow sin

From Google Map on the basis of latitude and longitude for the Earth's surface to achieve the distance between two points, slightly adapted for the domestic space distance calculation, the implementation has been sufficient to meters as a unit. net2.0,c# implementation.

public static double distanceoftwopoints (double lng1,double lat1, double lng2, double lat2, gausssphere GS)
{
Double RADLAT1 = Rad (LAT1);
Double radLat2 = Rad (LAT2);
Double A = RADLAT1-RADLAT2;
Double b = rad (lng1)-Rad (LNG2);
Double s = 2 * Math.asin (MATH.SQRT (Math.pow (Math.sin (A/2), 2) +
Math.Cos (RADLAT1) * MATH.COS (RADLAT2) * MATH.POW (Math.sin (B/2), 2));
s = S * (GS = = gausssphere.wgs84 6378137.0: (GS = = Gausssphere.xian80? 6,378,140:6,378,245));
s = Math.Round (S * 10000)/10000;
return s;
}

private static Double Rad (double D)
{
return d * math.pi/180.0;
}

Gausssphere for custom enumeration types
<summary>
The reference ellipsoid selected in the Gaussian projection
</summary>
public enum Gausssphere
{
BEIJING54,
Xian80,
WGS84,
}

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.