由于最近项目需要一些关于地理位置展示方面的需要,需要牵涉到地理位置方面与角度之间的计算。文章中参考了大神的代码,但是其是java代码实现的,无法在unity中直接使用。有兴趣的可以直接阅读原文http://blog.csdn.net/liang5630/article/details/42775997。
如图所示,A、B两点的经纬度已知,求其连线与经线 也就是与正北方向的角度。
首先,我们先定义一个经纬度类,
/// <summary> /// 经纬度类 /// </summary> /// public class MyLatLng {
public static double Rc=6378137; public static double Rj=6356725; public double m_LoDeg, m_LoMin, m_LoSec; public double m_LaDeg, m_LaMin, m_LaSec; public double m_Longitude, m_Latitude; public double m_RadLo,m_RadLa; public double Ec; public double Ed; public MyLatLng(double longitude,double latitude) { m_LoDeg=(int)longitude; m_LoMin=(int)((longitude-m_LoDeg)*60); m_LoSec=(longitude-m_LoDeg-m_LoMin/60)*3600; m_LaDeg=(int)latitude; m_LaMin=(int)((latitude-m_LaDeg)*60); m_LaSec=(latitude-m_LaDeg-m_LaMin/60)*3600; m_Longitude=longitude; m_Latitude=latitude; m_RadLo=longitude*Mathf.PI/180; m_RadLa=latitude*Mathf.PI/180; Ec=Rj+(Rc-Rj)*(90-m_Latitude)/90; Ed=Ec*(Mathf.Cos((float )m_RadLa)); } }
保存了经纬度,就可以在自己的类中实现 计算方法 返回的数值类型是一个double类型的数据。具体实现方法如下:定义了上面 经纬度类和 获取角度的方法,就可以进行角度计算了。/// <summary> /// 取得地图上两点的夹角 自己的写在A 别人的写在B /// </summary> /// <returns>The angle.</returns> /// <param name="A">A.</param> /// <param name="B">B.</param> public static double getAngle(MyLatLng A,MyLatLng B) { double dx=(B.m_RadLo-A.m_RadLo)*A.Ed; double dy=(B.m_RadLa-A.m_RadLa)*A.Ec; double angle=0.0; angle= Mathf.Atan (Mathf.Abs((float )dx/(float)dy)) *180/Mathf.PI; double dLo=B.m_Longitude-A.m_Longitude; double dLa=B.m_Latitude-A.m_Latitude; if(dLo>0&&dLa<=0) { angle=(90-angle)+90; } else if(dLo<=0&&dLa<0) { angle=angle+180; }else if(dLo<0&&dLa>=0) { angle= (90-angle)+270; } return angle; }
*************************************************************************************************************
实例使用如下:
假设我们已知A地 深圳大学的经纬度为:113.942909,22.540435,B地天安门的经纬度为116.232922,39.542637。我们在Start方法中实例化他们,并调用方法,代码如下:
运行程序,结果如下:void Start () { MyLatLngTest AShenZhenUniversity = new MyLatLngTest(113.942909, 22.540435); MyLatLngTest BTianAnMen = new MyLatLngTest(116.232922, 39.542637); double AngleAToB= getAngle(AShenZhenUniversity, BTianAnMen); double AngleBToA = getAngle(BTianAnMen , AShenZhenUniversity ); Debug.Log("深圳大学到北京天安门的角度是北偏"+AngleAToB +"度"); Debug.Log("北京天安门到深圳大学的角度为北偏" +AngleBToA +"度"); }
大功告成!
完整示例代码如下 新建脚本直接挂在摄像机上可以直接运行。
using UnityEngine; using System.Collections; using System; public class Controller_GetAngle : MonoBehaviour { // Use this for initialization void Start () { MyLatLngTest AShenZhenUniversity = new MyLatLngTest(113.942909, 22.540435); MyLatLngTest BTianAnMen = new MyLatLngTest(116.232922, 39.542637); double AngleAToB= getAngle(AShenZhenUniversity, BTianAnMen); double AngleBToA = getAngle(BTianAnMen , AShenZhenUniversity ); Debug.Log("深圳大学到北京天安门的角度是北偏"+AngleAToB +"度"); Debug.Log("北京天安门到深圳大学的角度为北偏" +AngleBToA +"度"); } // Update is called once per frame void Update () { } /// <summary> /// 取得地图上两点的夹角 自己的写在A 别人的写在B /// </summary> /// <returns>The angle.</returns> /// <param name="A">A.</param> /// <param name="B">B.</param> public static double getAngle(MyLatLngTest A, MyLatLngTest B) { double dx = (B.m_RadLo - A.m_RadLo) * A.Ed; double dy = (B.m_RadLa - A.m_RadLa) * A.Ec; double angle = 0.0; angle = Mathf.Atan (Mathf.Abs((float)dx / (float)dy)) * 180 / Mathf.PI; double dLo = B.m_Longitude - A.m_Longitude; double dLa = B.m_Latitude - A.m_Latitude; if (dLo > 0 && dLa <= 0) { angle = (90 - angle) + 90; } else if (dLo <= 0 && dLa < 0) { angle = angle + 180; } else if (dLo < 0 && dLa >= 0) { angle = (90 - angle) + 270; } return angle; } } /// <summary> /// 经纬度类 /// </summary> /// public class MyLatLngTest { public static double Rc = 6378137; public static double Rj = 6356725; public double m_LoDeg, m_LoMin, m_LoSec; public double m_LaDeg, m_LaMin, m_LaSec; public double m_Longitude, m_Latitude; public double m_RadLo, m_RadLa; public double Ec; public double Ed; public MyLatLngTest(double longitude, double latitude) { m_LoDeg = (int)longitude; m_LoMin = (int)((longitude - m_LoDeg) * 60); m_LoSec = (longitude - m_LoDeg - m_LoMin / 60) * 3600; m_LaDeg = (int)latitude; m_LaMin = (int)((latitude - m_LaDeg) * 60); m_LaSec = (latitude - m_LaDeg - m_LaMin / 60) * 3600; m_Longitude = longitude; m_Latitude = latitude; m_RadLo = longitude * Mathf.PI / 180; m_RadLa = latitude * Mathf.PI / 180; Ec = Rj + (Rc - Rj) * (90 - m_Latitude) / 90; Ed = Ec * (Mathf.Cos((float)m_RadLa)); } }