通过两个位置的经纬度坐标计算距离(C#版本)
////// 通过地图上的两个坐标计算距离(C#版本) /// Add by 成长的小猪(Jason.Song) on 2017/11/01 /// http://blog.csdn.net/jasonsong2008 /// public class MapHelper { /// /// 地球半径 /// private const double EarthRadius = 6378.137; /// /// 经纬度转化成弧度 /// Add by 成长的小猪(Jason.Song) on 2017/11/01 /// http://blog.csdn.net/jasonsong2008 /// /// /// private static double Rad(double d) { return d * Math.PI / 180d; } /// /// 计算两个坐标点之间的距离 /// Add by 成长的小猪(Jason.Song) on 2017/11/01 /// http://blog.csdn.net/jasonsong2008 /// /// 第一个坐标的纬度 /// 第一个坐标的经度 /// 第二个坐标的纬度 /// 第二个坐标的经度 /// 返回两点之间的距离,单位:公里/千米 public static double GetDistance(double firstLatitude, double firstLongitude, double secondLatitude, double secondLongitude) { var firstRadLat = Rad(firstLatitude); var firstRadLng = Rad(firstLongitude); var secondRadLat = Rad(secondLatitude); var secondRadLng = Rad(secondLongitude); var a = firstRadLat - secondRadLat; var b = firstRadLng - secondRadLng; var cal = 2 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin(a / 2), 2) + Math.Cos(firstRadLat) * Math.Cos(secondRadLat) * Math.Pow(Math.Sin(b / 2), 2))) * EarthRadius; var result = Math.Round(cal * 10000) / 10000; return result; } /// /// 计算两个坐标点之间的距离 /// Add by 成长的小猪(Jason.Song) on 2017/11/01 /// http://blog.csdn.net/jasonsong2008 /// /// 第一个坐标点的(纬度,经度) /// 第二个坐标点的(纬度,经度) /// 返回两点之间的距离,单位:公里/千米 public static double GetPointDistance(string firstPoint, string secondPoint) { var firstArray = firstPoint.Split(','); var secondArray = secondPoint.Split(','); var firstLatitude = Convert.ToDouble(firstArray[0].Trim()); var firstLongitude = Convert.ToDouble(firstArray[1].Trim()); var secondLatitude = Convert.ToDouble(secondArray[0].Trim()); var secondLongitude = Convert.ToDouble(secondArray[1].Trim()); return GetDistance(firstLatitude, firstLongitude, secondLatitude, secondLongitude); } }
调用方法:
////// 文章来源于成长的小猪 /// http://blog.csdn.net/jasonsong2008 /// /// static void Main(string[] args) { //第一种调用方法 var result = MapHelper.GetDistance(31.2553210000, 121.4620020000, 31.2005470000, 121.3269970000); //第二种调用方法 result = MapHelper.GetPointDistance("31.2553210000,121.4620020000", "31.2005470000,121.3269970000"); }