根据两点的经纬度坐标算出两点间距离和方位
完整的java代码,求地图两点经纬度的距离和方位,代码如下:
package org.test; public class LnglatEntity { public LnglatEntity() { } static double PI = Math.PI;//3.14159265 static double Rc = 6378137; // 赤道半径 static double Rj = 6356725; // 极半径 public double m_LoDeg, m_LoMin, m_LoSec; // longtitude 经度 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; // 构造函数, 经度: loDeg 度, loMin 分, loSec 秒; 纬度: laDeg 度, laMin 分, laSec秒 public LnglatEntity(double loDeg, double loMin, double loSec, double laDeg, double laMin, double laSec) { m_LoDeg = loDeg; m_LoMin = loMin; m_LoSec = loSec; m_LaDeg = laDeg; m_LaMin = laMin; m_LaSec = laSec; m_Longitude = m_LoDeg + m_LoMin / 60 + m_LoSec / 3600; m_Latitude = m_LaDeg + m_LaMin / 60 + m_LaSec / 3600; m_RadLo = m_Longitude * PI / 180.; m_RadLa = m_Latitude * PI / 180.; Ec = Rj + (Rc - Rj) * (90. - m_Latitude) / 90.; Ed = Ec * Math.cos(m_RadLa); } // ! public LnglatEntity(double longitude, double latitude) { m_LoDeg = (int) (longitude); m_LoMin = (int) ((longitude - m_LoDeg) * 60); m_LoSec = (int) (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 * PI / 180.; m_RadLa = latitude * PI / 180.; Ec = Rj + (Rc - Rj) * (90. - m_Latitude) / 90.; Ed = Ec * Math.cos(m_RadLa); } }
LngLatHelper类:
package org.test; import java.util.HashMap; import java.util.Map; public class LngLatHelper { // ! 计算点A 和 点B的经纬度,求他们的距离和点B相对于点A的方位 /* * ! /param A A点经纬度 /param B B点经纬度 /param angle B相对于A的方位, 不需要返回该值,则将其设为空 * /return A点B点的距离 */ static Map<String, Double> distance(LnglatEntity A, LnglatEntity B) { Map<String, Double> result = new HashMap<String, Double>(); double dx = (B.m_RadLo - A.m_RadLo) * A.Ed; double dy = (B.m_RadLa - A.m_RadLa) * A.Ec; double out = Math.sqrt(dx * dx + dy * dy); double angle; 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; } result.put("distance", out / 1000); result.put("angle", angle); return result; } // ! 计算点A 和 点B的经纬度,求他们的距离和点B相对于点A的方位 /* * ! /param longitude1 A点经度 /param latitude1 A点纬度 /param longitude2 B点经度 * /param latitude2 B点纬度 /param angle B相对于A的方位, 不需要返回该值,则将其设为空 /return * A点B点的距离 */ static Map<String, Double> distance(double longitude1, double latitude1, double longitude2, double latitude2) { LnglatEntity A = new LnglatEntity(longitude1, latitude1); LnglatEntity B = new LnglatEntity(longitude2, latitude2); return distance(A, B); } // ! 已知点A经纬度,根据B点据A点的距离,和方位,求B点的经纬度 /* * ! /param A 已知点A /param distance B点到A点的距离 /param angle B点相对于A点的方位 /return * B点的经纬度坐标 */ static LnglatEntity GetlnglatByDis(LnglatEntity A, double distance, double angle) { double dx = distance * 1000 * Math.sin(angle * Math.PI / 180.); double dy = distance * 1000 * Math.cos(angle * Math.PI / 180.); // double dx = (B.m_RadLo - A.m_RadLo) * A.Ed; // double dy = (B.m_RadLa - A.m_RadLa) * A.Ec; double BJD = (dx / A.Ed + A.m_RadLo) * 180. / Math.PI; double BWD = (dy / A.Ec + A.m_RadLa) * 180. / Math.PI; LnglatEntity B = new LnglatEntity(BJD, BWD); return B; } // ! 已知点A经纬度,根据B点据A点的距离,和方位,求B点的经纬度 /* * ! /param longitude 已知点A经度 /param latitude 已知点A纬度 /param distance B点到A点的距离 * /param angle B点相对于A点的方位 /return B点的经纬度坐标 */ static LnglatEntity GetlnglatByDis(double longitude, double latitude, double distance, double angle) { LnglatEntity A = new LnglatEntity(longitude, latitude); return GetlnglatByDis(A, distance, angle); } }
LatlngTest 测试类:
package org.test; import java.util.Map; public class LatlngTest { /** * @param args */ public static void main(String[] args) { // // TODO Auto-generated method stub // double ss =GetDistance(32, 120, 31, 120); // System.out.println(ss); //116.377535,39.921576 //116.383787,39.921188 double jd1 = 116.377535; double wd1 = 39.921576;// 成都 double jd2 = 116.383787; double wd2 = 39.921188; // double jd1 = 10; // double wd1 = 10;// 成都 // double jd2 = 10; // double wd2 = 20; Map<String, Double> result = LngLatHelper.distance(jd1,wd1,jd2,wd2); double distance =result.get("distance"); double angle =result.get("angle"); System.out.println("距离:"+distance); System.out.println("角度:"+angle); angle = +90; LnglatEntity entity = LngLatHelper.GetlnglatByDis(jd1, wd1, distance, angle); System.out.println(entity.m_Longitude); System.out.println(entity.m_Latitude); } }
来源://作者:/更新时间:2013-01-31
顶
踩
相关文章: