반응형
public class LocationDistance {

	public static void main(String[] args) {

		// 마일(Mile) 단위
		double distanceMile = 
			distance(37.504198, 127.047967, 37.501025, 127.037701, "");
		
		// 미터(Meter) 단위
		double distanceMeter = 
			distance(37.49268,126.78442,37.59084,126.78918, "meter");
		
		// 킬로미터(KiloMeter) 단위
		double distanceKiloMeter = 
			distance(37.49268,126.78442,37.59084,126.78918, "kilometer");
		
		System.out.println(distanceMile) ;
		System.out.println(distanceMeter) ;
		System.out.println(distanceKiloMeter) ;
		
	}
	
	
	/**
	 * 두 지점간의 거리 계산
	 * 
	 * @param shoptLat 지점 1 위도
	 * @param shopLngt 지점 1 경도 
	 * @param userLat 지점 2 위도
	 * @param lon2 지점 2 경도
	 * @param unit 거리 표출단위 
	 * @return
	 */
	
	
	private static double distance(double shoptLat, double shopLngt, double userLat, double lon2, String unit) {
		
		double theta = shopLngt - lon2;
		double dist = Math.sin(deg2rad(shoptLat)) * Math.sin(deg2rad(userLat)) 
				+ Math.cos(deg2rad(shoptLat)) * Math.cos(deg2rad(userLat)) * Math.cos(deg2rad(theta));
		
		dist = Math.acos(dist);
		dist = rad2deg(dist);
		dist = dist * 60 * 1.1515;
		
		if (unit == "kilometer") {
			dist = dist * 1.609344;
		} else if(unit == "meter"){
			dist = dist * 1609.344;
		} 

		return (dist);
	}
	

	// This function converts decimal degrees to radians
	private static double deg2rad(double deg) {
		return (deg * Math.PI / 180.0);
	}
	
	// This function converts radians to decimal degrees
	private static double rad2deg(double rad) {
		return (rad * 180 / Math.PI);
	}
}

 

출처 : https://m.blog.naver.com/PostView.naver?isHttpsRedirect=true&blogId=akdlakdrn&logNo=221717580886 

반응형