자바&데이터 베 이 스 는 두 좌표 의 거 리 를 계산한다.


1.자바 로 계산 하기
 
/**
 *      :           
 * Title: DistanceUtil.java
 * @author liuxing
 * @date 2013-9-8   10:36:03
 * @version V1.0
 */
public class DistanceUtil {

	private static double DEF_PI = Math.PI; // PI
	private static double DEF_2PI = Math.PI * 2; // 2*PI
	private static double DEF_PI180 = Math.PI / 180; // PI/180.0
	private static double DEF_R = 6370693.5; //     

	/**
	 * 
	 *     :              (     )
	 * liuxing 2013-9-8   10:42:17
	 * @param lng1     
	 * @param lat1     
	 * @param lng2     
	 * @param lat2     
	 * @return
	 */
	public static double getShortDistance(double lng1, double lat1, double lng2, double lat2) {
		double ew1, ns1, ew2, ns2;
		double dx, dy, dew;
		double distance;
		//        
		ew1 = Math.toRadians(lng1);
		ns1 = Math.toRadians(lat1);
		ew2 = Math.toRadians(lng2);
		ns2 = Math.toRadians(lat2);
		
		//    
		dew = ew1 - ew2;
		//        180  ,    
		if (dew > DEF_PI){
			dew = DEF_2PI - dew;
		} else if (dew < -DEF_PI){
			dew = DEF_2PI + dew;
		}
			
		dx = DEF_R * Math.cos(ns1) * dew; 	//       (          )
		dy = DEF_R * (ns1 - ns2); 			//       (          )
		//         
		distance = Math.sqrt(dx * dx + dy * dy);
		return distance;
	}

	/**
	 * 
	 *     :              (     )
	 * liuxing 2013-9-8   10:43:21
	 * @param lng1     
	 * @param lat1     
	 * @param lng2     
	 * @param lat2     
	 * @return
	 */
	public static double getLongDistance(double lng1, double lat1, double lng2, double lat2) {
		double ew1, ns1, ew2, ns2;
		double distance;
		
		//        
		ew1 = lng1 * DEF_PI180;
		ns1 = lat1 * DEF_PI180;
		ew2 = lng2 * DEF_PI180;
		ns2 = lat2 * DEF_PI180;
		
		//             (  )
		distance = Math.sin(ns1) * Math.sin(ns2) + Math.cos(ns1) * Math.cos(ns2) * Math.cos(ew1 - ew2);
		//    [-1..1]   ,    
		if (distance > 1.0){
			distance = 1.0;
		} else if (distance < -1.0){
			distance = -1.0;
		}
		
		//        
		distance = DEF_R * Math.acos(distance);
		return distance;
	}
	
	public static void main(String[] args) {
		double mLat1 = 31.24081800000000; 	// point1  
		double mLng1 = 121.46541700000000; 	// point1  
		double mLat2 = 31.239946;	// point2  
		double mLng2 = 121.466417;	// point2  
		
		double distanceByShort = getShortDistance(mLng1, mLat1, mLng2, mLat2);
		System.out.println(distanceByShort);
		
		double distanceByLong = getLongDistance(mLng1, mLat1, mLng2, mLat2);
		System.out.println(distanceByLong);
	}

}

 
 
2.SqlServer 함수 로 계산
    다른 데이터베이스 버 전 은 해당 함 수 를 찾 아 교체 한 후 이식 하 십시오.
 
-- =============================================
-- Author:      liuxing
-- Create date: 2013-09-10
-- Description:	  2       (     )
-- =============================================
CREATE function dbo.fn_getShortDistance(
	 @lng1 decimal(19,11)
	,@lat1 decimal(19,11)
	,@lng2 decimal(19,11)
	,@lat2 decimal(19,11)
)
returns decimal(19,11)
AS
BEGIN
	--declare @lng1 decimal(19,11)
	--declare @lat1 decimal(19,11)
	--declare @lng2 decimal(19,11)
	--declare @lat2 decimal(19,11)


	--set @lat1 = 31.238662--; 	// point1  
	--set @lng1 = 121.466633--; // point1  
	--set @lat2 = 31.239727--;	// point2  
	--set @lng2 = 121.462745--;	// point2  
	declare @ew1 decimal(19,11)
		, @ns1 decimal(19,11)
		, @ew2 decimal(19,11)
		, @ns2 decimal(19,11)
		, @dx decimal(19,11)
		, @dy decimal(19,11)
		, @dew decimal(19,11)
		, @distance decimal(19,11)
	--        
	set @ew1 = Radians(@lng1)-- * 0.01745329252;
	set @ns1 = Radians(@lat1)-- * 0.01745329252;
	set @ew2 = Radians(@lng2)-- * 0.01745329252;
	set @ns2 = Radians(@lat2)-- * 0.01745329252;
	--    
	set @dew = @ew1 - @ew2;
	--        180  ,    
	if (@dew > Pi())
	begin
		set @dew = 2 * Pi() - @dew;
	end
	else if (@dew < -Pi())
	begin
		set @dew = 2 * Pi() + @dew;
	end
		
	set @dx = 6370693.5 * Cos(@ns1) * @dew --       (          )
	set @dy = 6370693.5 * (@ns1 - @ns2)   --       (          )
	--         ,    
	set @distance = sqrt(@dx * @dx + @dy * @dy);
	return @distance;
	--print @distance
END

 

좋은 웹페이지 즐겨찾기