根据高德地图两点坐标获取两点实际距离,用于高德地图标记点范围内距离计算
/// <summary>
/// 获取高德地图两坐标点间的距离
/// </summary>
/// <param name="lat1">点1横坐标</param>
/// <param name="lng1">点1纵坐标</param>
/// <param name="lat2">点2横坐标</param>
/// <param name="lng2">点2纵坐标</param>
/// <returns></returns>
public static double GetDistance(double lat1, double lng1, double lat2, double lng2)
{
try
{
var b = Math.PI / 180;
var c = Math.Sin((lat2 - lat1) * b / 2);
var d = Math.Sin((lng2 - lng1) * b / 2);
var a = c * c + d * d * Math.Cos(lat1 * b) * Math.Cos(lat2 * b);
return 12756274 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
}
catch (Exception)
{
return 0;
}
}
网友评论