1. 程式人生 > 其它 >通過地圖上的兩個座標計算距離

通過地圖上的兩個座標計算距離

/// <summary>
    /// 通過地圖上的兩個座標計算距離(C#版本)
    /// Add by 成長的小豬(Jason.Song) on 2017/11/01
    /// http://blog.csdn.net/jasonsong2008
    /// </summary>
    public class MapHelper
    {
        /// <summary>
        /// 地球半徑
        /// </summary>
        private const double EarthRadius = 6378.137;

        /// <summary>
/// 經緯度轉化成弧度 /// Add by 成長的小豬(Jason.Song) on 2017/11/01 /// http://blog.csdn.net/jasonsong2008 /// </summary> /// <param name="d"></param> /// <returns></returns> private static double Rad(double d) { return d * Math.PI / 180d; }
/// <summary> /// 計算兩個座標點之間的距離 /// Add by 成長的小豬(Jason.Song) on 2017/11/01 /// http://blog.csdn.net/jasonsong2008 /// </summary> /// <param name="firstLatitude">第一個座標的緯度</param> /// <param name="firstLongitude">第一個座標的經度</param> /// <param name="secondLatitude">
第二個座標的緯度</param> /// <param name="secondLongitude">第二個座標的經度</param> /// <returns>返回兩點之間的距離,單位:公里/千米</returns> 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; } //地球半徑,單位米 private const double EARTH_RADIUS = 6378137; /// <summary> /// 計算兩點位置的距離,返回兩點的距離,單位 米 /// 該公式為GOOGLE提供,誤差小於0.2米 /// </summary> /// <param name="lat1">第一點緯度</param> /// <param name="lng1">第一點經度</param> /// <param name="lat2">第二點緯度</param> /// <param name="lng2">第二點經度</param> /// <returns>返回距離(米)</returns> public static double GetDistance2(double lat1, double lng1, double lat2, double lng2) { double radLat1 = Rad(lat1); double radLng1 = Rad(lng1); double radLat2 = Rad(lat2); double radLng2 = Rad(lng2); double a = radLat1 - radLat2; double b = radLng1 - radLng2; double result = 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))) * EARTH_RADIUS; return result; } /// <summary> /// 計算兩個座標點之間的距離 /// Add by 成長的小豬(Jason.Song) on 2017/11/01 /// http://blog.csdn.net/jasonsong2008 /// </summary> /// <param name="firstPoint">第一個座標點的(緯度,經度)</param> /// <param name="secondPoint">第二個座標點的(緯度,經度)</param> /// <returns>返回兩點之間的距離,單位:公里/千米</returns> 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); } }

https://www.cnblogs.com/chenkh/p/5661097.html

https://www.bbsmax.com/A/amd0QjZjzg/