经纬度求距离求与正北方向的夹角(方向角):
ackage com.liang.test;
public class AngleUtil {
public static void main(String[] args) {
MyLatLng A=new MyLatLng(113.249648,23.401553);
MyLatLng B=new MyLatLng(113.246033,23.403362);
System.out.println(getAngle(A,B));
}
/**
* 求B点经纬度
* @param A 已知点的经纬度,
* @param distance AB两地的距离 单位km
* @param angle AB连线与正北方向的夹角(0~360)
* @return B点的经纬度
*/
public static MyLatLng getMyLatLng(MyLatLng A,double distance,double angle){
double dx = distance*1000*Math.sin(Math.toRadians(angle));
double dy= distance*1000*Math.cos(Math.toRadians(angle));
double bjd=(dx/A.Ed+A.m_RadLo)*180./Math.PI;
double bwd=(dy/A.Ec+A.m_RadLa)*180./Math.PI;
return new MyLatLng(bjd, bwd);
}
/**
* 获取AB连线与正北方向的角度
* @param A A点的经纬度
* @param B B点的经纬度
* @return AB连线与正北方向的角度(0~360)
*/
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=Math.atan(Math.abs(dx/dy))*180./Math.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;
}
static class MyLatLng {
final static double Rc=6378137;
final static double Rj=6356725;
double m_LoDeg,m_LoMin,m_LoSec;
double m_LaDeg,m_LaMin,m_LaSec;
double m_Longitude,m_Latitude;
double m_RadLo,m_RadLa;
double Ec;
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*Math.PI/180.;
m_RadLa=latitude*Math.PI/180.;
Ec=Rj+(Rc-Rj)*(90.-m_Latitude)/90.;
Ed=Ec*Math.cos(m_RadLa);
}
}
}
/**
*
* @param long1 经度1
* @param lat1 维度1
* @param long2 经度2
* @param lat2 纬度2
* @return
*/
public static double getDistance(double long1, double lat1, double long2,
double lat2) {
double a, b, R;
R = 6378137; // 地球半径
lat1 = lat1 * Math.PI / 180.0;
lat2 = lat2 * Math.PI / 180.0;
a = lat1 - lat2;
b = (long1 - long2) * Math.PI / 180.0;
double d;
double sa2, sb2;
sa2 = Math.sin(a / 2.0);
sb2 = Math.sin(b / 2.0);
d = 2 * R * Math.asin(Math.sqrt(sa2 * sa2 + Math.cos(lat1) * Math.cos(lat2) * sb2 * sb2));
return d;
}
/**
*
* @param lat_a 纬度1
* @param lng_a 经度1
* @param lat_b 纬度2
* @param lng_b 经度2
* @return
*/
private double getAngle1(double lat_a, double lng_a, double lat_b, double lng_b) {
double y = Math.sin(lng_b-lng_a) * Math.cos(lat_b);
double x = Math.cos(lat_a)*Math.sin(lat_b) - Math.sin(lat_a)*Math.cos(lat_b)*Math.cos(lng_b-lng_a);
double brng = Math.atan2(y, x);
brng = Math.toDegrees(brng);
if(brng < 0)
brng = brng +360;
return brng;
}
已知起点经纬度、距离、方位角,计算终点经纬度(matlab)
function [JJ,WW,Ang21]=disjw(Jd,Wd,Dis,Angle)
% [JJ,WW,Ang21]=disjw(Jd,Wd,Dis,Angle)
% 此函数已知两点间距Dis(以米为单位)、与正北夹角Angle(以°为单位)和一点的经纬度(Jd,Wd),
% 计算另一点的经纬度(JJ,WW)
e=2.718282;
re=6371000; %
a=6378245;
e2=0.0067385241;
jd=Jd*pi/180;
wd=Wd*pi/180;
angle=Angle*pi/180;
A=sqrt(1+e2*cos(wd)^4);
B=sqrt(1+e2*cos(wd)^2);
C=sqrt(1+e2);
cgm=Dis*B^2/(a*C);
temp_x=B*cos(wd)-tan(cgm)*sin(wd)*cos(angle);
% if(temp_x<0.001)
% temp_x=0.001;
jj=jd+(1/A)*atan(A*tan(cgm)*sin(angle)/temp_x);
W=A*(jj-jd)/2;
x=cos(angle);
y=(1/A)*sin(wd)*sin(angle)*tan(W);
z=sin(cgm)*(x-y);
D=0.5*asin(z);
ww=wd+2*D*(B-(3.0/2.0)*e2*D*sin(2*wd+(4.0/3.0)*B*D));
E=cos(cgm)*(tan(cgm)*tan(wd)-B*cos(angle));
F=-B*sin(angle);
ang21=atan2(F,E);
if ang21<0
ang21=ang21+2*pi;
end
JJ=jj*180/pi;
WW=ww*180/pi;
Ang21=ang21*180/pi;