获取2个经纬度坐标之间的距离

获取地球 2个经纬度之间的距离

JAVA源码如下:

private  double EARTH_RADIUS = 6378.137;  //地球半径

private  double rad(double d)
{
return d * Math.PI / 180.0;     //计算弧长
}

public  double GetDistance(double Lat1,double Lon1,double Lat2,double Lon2 ){
	double radLat1 = rad(Lat1);
	double radLat2 = rad(Lat2);
	double a =radLat1 - radLat2;
	double b =rad(Lon1) - rad(Lon2);
	double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) + Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
	s = s * EARTH_RADIUS;
	BigDecimal bg = new BigDecimal(s);
	double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
	return f1;
}

结果保留2位小数 单位为km

SQL源码如下:

lon1: 122.3 lat1: 37.1
lon2: 122.2 lat2: 37.6

select ROUND(
6378.138*2*ASIN(
SQRT(
	POW(
		SIN(
		(	(PI()*37.1/180.0)
			-
			(PI()*37.6/180.0)
		)
		/2)
	,2)
	+
	COS(
		(PI()*37.1/180.0)
	)
	*
	COS(
		(PI()*37.6/180.0)
	)
	*
	POW(
		SIN(
		(
			(PI()*122.3/180.0)
			-
			(PI()*122.2/180.0)
		)/2)
	,2)
	)
),2) as langth;


其中lon1,lon2为经度 而lat1,lat2为纬度  单位为千米

你可能感兴趣的:(获取2个经纬度坐标之间的距离)