码迷,mamicode.com
首页 > 其他好文 > 详细

经纬度相关的一些计算

时间:2014-12-22 18:05:59      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:经纬度

(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;
	}




经纬度相关的一些计算

标签:经纬度

原文地址:http://blog.csdn.net/dongyi91/article/details/42081451

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!