GIS中通过两点经纬度确定方位角与方位
确定逆向地理编码时服务商对地址的方位没有清楚的描述,导致偏远的地区没有明确的描述,此算法通过两个坐标的相对位置计算出了方位角得到方位,可以清楚的描述 A地址距离B地址南北方向5000米,类似这样的说明。
此代码为后端C#代码,也是就是这个,灵魂已有,各自实施。
public static class LatLonGetDirection
{
/// <summary>
/// 根据经纬度计算角度
/// </summary>
/// <param name="lat1"></param>
/// <param name="lng1"></param>
/// <param name="lat2"></param>
/// <param name="lng2"></param>
/// <returns></returns>
public static double GetJiaoDu(location ll, location ll2)
{
double x1 = Convert.ToDouble(ll.lng);
double y1 = Convert.ToDouble(ll.lat);
double x2 = Convert.ToDouble(ll2.lng);
double y2 = Convert.ToDouble(ll2.lat);
double pi = Math.PI;
double w1 = y1 / 180 * pi;
double j1 = x1 / 180 * pi;
double w2 = y2 / 180 * pi;
double j2 = x2 / 180 * pi;
double ret;
if (j1 == j2)
{
if (w1 > w2) return 270; //北半球的情况,南半球忽略
else if (w1 < w2) return 90;
else return -1;//位置完全相同
}
ret = 4 * Math.Pow(Math.Sin((w1 - w2) / 2), 2) - Math.Pow(Math.Sin((j1 - j2) / 2) * (Math.Cos(w1) - Math.Cos(w2)), 2);
ret = Math.Sqrt(ret);
double temp = (Math.Sin(Math.Abs(j1 - j2) / 2) * (Math.Cos(w1) + Math.Cos(w2)));
ret = ret / temp;
ret = Math.Atan(ret) / pi * 180;
if (j1 > j2) // 1为参考点坐标
{
if (w1 > w2) ret += 180;
else ret = 180 - ret;
}
else if (w1 > w2) ret = 360 - ret;
return ret;
}
/// <summary>
/// 计算方位 按360计算
/// </summary>
/// <param name="lat1">参照物纬度</param>
/// <param name="lng1">参照物经度</param>
/// <param name="lat2">目标纬度</param>
/// <param name="lng2">目标经度</param>
/// <returns></returns>
public static string GetDirection(location ll, location ll2)
{
double jiaodu = GetJiaoDu(ll, ll2);
if ((jiaodu <= 10) || (jiaodu > 350)) return "东";
if ((jiaodu > 10) && (jiaodu <= 80)) return "东北";
if ((jiaodu > 80) && (jiaodu <= 100)) return "北";
if ((jiaodu > 100) && (jiaodu <= 170)) return "西北";
if ((jiaodu > 170) && (jiaodu <= 190)) return "西";
if ((jiaodu > 190) && (jiaodu <= 260)) return "西南";
if ((jiaodu > 260) && (jiaodu <= 280)) return "南";
if ((jiaodu > 280) && (jiaodu <= 350)) return "东南";
return string.Empty;
}
}
转载自:https://blog.csdn.net/u012539364/article/details/75069134