经纬度相关的一些计算

欢迎访问我的个人博客: sky的技术小屋

(1)已知两个位置的经纬度,计算其间地理距离。

private static Double CalculateDistance(ArrayList<Double> latAndLngUser1,
			ArrayList<Double> latAndLngUser2) {
		// TODO Auto-generated method stub
		Double EARTH_RADIUS = 6378.137;
		
		Double radLat1 = latAndLngUser1.get(0) * Math.PI / 180.0;
		Double radLat2 = latAndLngUser2.get(0) * Math.PI / 180.0;
		Double a = radLat1 - radLat2;
		Double b = latAndLngUser1.get(1) * Math.PI / 180.0 - latAndLngUser2.get(1) * Math.PI / 180.0;
		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;
		s = Math.round(s * 10000.0) / 10000.0;
		return s;
	}

(2)已知多个位置的经纬度数组,计算这些位置的地理中心。

	private static ArrayList<Double> CalculateCenter(
			ArrayList<ArrayList<Double>> latAndLngArray) {
		// TODO Auto-generated method stub
		ArrayList<Double> center = new ArrayList<Double>();
		Double X = 0.0, Y = 0.0, Z = 0.0;
		for (int i = 0; i < latAndLngArray.size(); i++) {
			Double lat = latAndLngArray.get(i).get(0) * Math.PI / 180;
			Double lng = latAndLngArray.get(i).get(1) * Math.PI / 180;
			X += Math.cos(lat) * Math.cos(lng);
			Y += Math.cos(lat) * Math.sin(lng);
			Z += Math.sin(lat);
		}
		X = X / latAndLngArray.size();
		Y = Y / latAndLngArray.size();
		Z = Z / latAndLngArray.size();
		Double A = Math.atan2(Y, X);
		Double B = Math.sqrt(X * X + Y * Y);
		Double C = Math.atan2(Z, B);
		Double centerLng = A * 180 / Math.PI;
		Double centerLat = C * 180 / Math.PI;
		center.add(centerLat);
		center.add(centerLng);
		return center;
	}

猜你喜欢

转载自blog.csdn.net/dy01dy/article/details/42081451
今日推荐