虽然看过多遍,但只有自己亲自踩过坑才记忆深刻

毕设代码计算地球两点间距离,经纬度计算距离, 在
return Math.sqrt(latDistance*latDistance + longDistance*longDistance);
两个int相乘溢出了
	private static int HOUR_DISTANCE = 111000;
	private static int MIN_DISTANCE = 1850;
	private static int SEC_DISTANCE = 30;
	//计算两个点的距离
	public static double getDistance(double lat1, double lng1, double lat2, double lng2)
	{
		int hour, min, sec;
		int hour2, min2, sec2;
		
		int latDistance, longDistance;
		
		hour = (int)(lat1);
		min = (int)((lat1-hour)*60);
		sec = (int)(((lat1-hour)*60 - min)*60);
		
		hour2 = (int)(lat2);
		min2 = (int)((lat2-hour2)*60);
		sec2 = (int)(((lat2-hour2)*60 - min2)*60);
		latDistance = Math.abs(hour-hour2) * HOUR_DISTANCE 
					+ Math.abs(min-min2) * MIN_DISTANCE
					+ Math.abs(sec-sec2) * SEC_DISTANCE;

		hour = (int)(lng1);
		min = (int)((lng1-hour)*60);
		sec = (int)(((lng1-hour)*60 - min)*60);
		
		hour2 = (int)(lng2);
		min2 = (int)((lng2-hour2)*60);
		sec2 = (int)(((lng2-hour2)*60 - min2)*60);
		longDistance = Math.abs(hour-hour2) * HOUR_DISTANCE 
					+ Math.abs(min-min2) * MIN_DISTANCE
					+ Math.abs(sec-sec2) * SEC_DISTANCE;
		
		return Math.sqrt(latDistance*latDistance + longDistance*longDistance);
	}

Published: June 01 2012

  • tags: