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

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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值