JAVA实现 百度两点间的距离 getDistance

来源:互联网 发布:手机指南针软件 编辑:程序博客网 时间:2024/05/16 17:21
import java.awt.geom.Point2D;public class Demo {public static void main(String[] args) {Point2D.Double point1 = new Point2D.Double(106.486654,29.490295);Point2D.Double point2 = new Point2D.Double(106.581515,29.615467);System.out.println(getDistance(point1, point2));}/** * JAVA实现  百度两点间的距离 LH * -结果与百度JsApi的getDistance一致 * @param point1 * @param point2 * @return */public static double getDistance(Point2D.Double point1,Point2D.Double point2){return Wv(point1, point2);}private static double Wv(Point2D.Double a,Point2D.Double b){if(a==null || b==null){return 0;}double a_lng = ew(a.x, -180, 180);  double a_lat = lw(a.y, -74, 74);  double b_lng = ew(b.x, -180, 180);  double b_lat = lw(b.y, -74, 74);  return Td(oi(a_lng), oi(b_lng), oi(a_lat), oi(b_lat));}private static double oi(double a){ return Math.PI * a / 180;}private static double Td(double a,double b,double c,double d){return 6370996.81 * Math.acos(Math.sin(c) * Math.sin(d) + Math.cos(c) * Math.cos(d) * Math.cos(b - a));}private static double ew(double a,double b,double c){if(a>c){ a -= c - b ; }else if(a<b){ a += c - b;}    return a ;}private static double lw(double a,double b,double c){a = max(a,b) ;a = min(a,c);return a;}private static double max(double a,double b){ if(a>b){  return a ; }   return b ;}private static double min(double a,double c){if(a>c){ return c ;}      return a ;}}

0 0