您的位置:首页 > 编程语言 > Java开发

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

2016-10-24 16:46 204 查看
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 ;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息