`

java根据经纬度计算两点距离(java 代码 sql 代码)

 
阅读更多
项目上正好用到 。顺便贴给JE们。
public double distance(double n1, double e1, double n2, double e2)
     {
         double jl_jd = 102834.74258026089786013677476285//每经度单位米;
         double jl_wd = 111712.69150641055729984301412873//每纬度单位米;
         double b = Math.abs((e1 - e2) * jl_jd);
         double a = Math.abs((n1 - n2) * jl_wd);
         return Math.sqrt((a * a + b * b));

     }



ALTER FUNCTION dbo.fnGetDistance  

 (  
     @LatBegin REAL 

    , @LngBegin REAL 

     , @LatEnd REAL 

     , @LngEnd REAL 

 )  

 RETURNS FLOAT 

 AS 

 BEGIN 

     DECLARE @Distance REAL 

     DECLARE @EARTH_RADIUS REAL 

     SET @EARTH_RADIUS = 6378.137  

        

     DECLARE @RadLatBegin REAL, @RadLatEnd REAL, @RadLatDiff REAL, @RadLngDiff REAL 

     SET @RadLatBegin = @LatBegin * PI() / 180.0  

     SET @RadLatEnd = @LatEnd * PI() / 180.0  

     SET @RadLatDiff = @RadLatBegin - @RadLatEnd  

     SET @RadLngDiff = @LngBegin * PI() / 180.0 - @LngEnd * PI() / 180.0  

        

     SET @Distance = 2 * ASIN(SQRT(POWER(Sin(@RadLatDiff / 2), 2) + COS(@RadLatBegin) * COS(@RadLatEnd) * POWER(SIN(@RadLngDiff/2),2)))  

     SET @Distance = @Distance * @EARTH_RADIUS  

     --SET @Distance = Round(@Distance * 10000) / 10000  

        

     RETURN @Distance  

 END 

 



/**
	 * @param longitude 经度
	 * @param latitude纬度
	 * @param range   多少范围内的
**/

	public Integer getWhereInfo(double longitude,double latitude,double range){
		
		StringBuffer hql = new StringBuffer();
		double jl_jd = 102834.74258026089786013677476285;
        double jl_wd = 111712.69150641055729984301412873;
        hql.append("from whereInfo w where  sqrt(power((w.longitude-"+longitude+")*"+jl_jd+",2)+power((w.latitude-"+latitude+")*"+jl_wd+",2))<="+range);
        return this.whereInfoDao.getwhereInfo(hql.toString());
		
	}
分享到:
评论
8 楼 JMS_Exception 2012-06-15  
JMS_Exception 写道
double jl_jd = 102834.74258026089786013677476285;  
        double jl_wd = 111712.69150641055729984301412873;
shtwtina 写道
請問得出來的距離單位是千米嗎?

shtwtina 写道
請問得出來的距離單位是千米嗎?

102834

double jl_jd : 102834.74258026089786013677476285 米;   
double jl_wd : 111712.69150641055729984301412873 米; 



单位米
7 楼 JMS_Exception 2012-06-15  
double jl_jd = 102834.74258026089786013677476285;  
        double jl_wd = 111712.69150641055729984301412873;
shtwtina 写道
請問得出來的距離單位是千米嗎?

shtwtina 写道
請問得出來的距離單位是千米嗎?

102834

double jl_jd : 102834.74258026089786013677476285 米;   
double jl_wd : 111712.69150641055729984301412873 米; 
6 楼 shtwtina 2012-04-24  
請問得出來的距離單位是千米嗎?
5 楼 JMS_Exception 2011-06-09  
orchidblue 写道
JMS_Exception 写道
xiaohan1026 写道
这是不久前在谷歌地图上做二次开发用到的.

private long calDistance(Double a_x_point, Double a_y_point,
			Double b_x_point, Double b_y_point) {
		Double R = new Double(6371);
		Double dlat = (b_x_point - a_x_point) * Math.PI / 180;
		Double dlon = (b_y_point - a_y_point) * Math.PI / 180;
		Double aDouble = Math.sin(dlat / 2) * Math.sin(dlat / 2)
				+ Math.cos(a_x_point * Math.PI / 180)
				* Math.cos(b_x_point * Math.PI / 180) * Math.sin(dlon / 2)
				* Math.sin(dlon / 2);
		Double cDouble = 2 * Math.atan2(Math.sqrt(aDouble), Math
				.sqrt(1 - aDouble));
		long d = Math.round((R * cDouble) * 1000) / 1000;
		return d;

	}

为什么 返回long  处理呢 。  

北京离上海差不多有1000多km,总不至于说有1,000,000.xxxxxxxxxm 吧

解?
4 楼 orchidblue 2011-05-30  
JMS_Exception 写道
xiaohan1026 写道
这是不久前在谷歌地图上做二次开发用到的.

private long calDistance(Double a_x_point, Double a_y_point,
			Double b_x_point, Double b_y_point) {
		Double R = new Double(6371);
		Double dlat = (b_x_point - a_x_point) * Math.PI / 180;
		Double dlon = (b_y_point - a_y_point) * Math.PI / 180;
		Double aDouble = Math.sin(dlat / 2) * Math.sin(dlat / 2)
				+ Math.cos(a_x_point * Math.PI / 180)
				* Math.cos(b_x_point * Math.PI / 180) * Math.sin(dlon / 2)
				* Math.sin(dlon / 2);
		Double cDouble = 2 * Math.atan2(Math.sqrt(aDouble), Math
				.sqrt(1 - aDouble));
		long d = Math.round((R * cDouble) * 1000) / 1000;
		return d;

	}

为什么 返回long  处理呢 。  

北京离上海差不多有1000多km,总不至于说有1,000,000.xxxxxxxxxm 吧
3 楼 javaliver 2011-05-30  
copy 
2 楼 JMS_Exception 2011-04-27  
xiaohan1026 写道
这是不久前在谷歌地图上做二次开发用到的.

private long calDistance(Double a_x_point, Double a_y_point,
			Double b_x_point, Double b_y_point) {
		Double R = new Double(6371);
		Double dlat = (b_x_point - a_x_point) * Math.PI / 180;
		Double dlon = (b_y_point - a_y_point) * Math.PI / 180;
		Double aDouble = Math.sin(dlat / 2) * Math.sin(dlat / 2)
				+ Math.cos(a_x_point * Math.PI / 180)
				* Math.cos(b_x_point * Math.PI / 180) * Math.sin(dlon / 2)
				* Math.sin(dlon / 2);
		Double cDouble = 2 * Math.atan2(Math.sqrt(aDouble), Math
				.sqrt(1 - aDouble));
		long d = Math.round((R * cDouble) * 1000) / 1000;
		return d;

	}

为什么 返回long  处理呢 。  
1 楼 xiaohan1026 2011-04-27  
这是不久前在谷歌地图上做二次开发用到的.

private long calDistance(Double a_x_point, Double a_y_point,
			Double b_x_point, Double b_y_point) {
		Double R = new Double(6371);
		Double dlat = (b_x_point - a_x_point) * Math.PI / 180;
		Double dlon = (b_y_point - a_y_point) * Math.PI / 180;
		Double aDouble = Math.sin(dlat / 2) * Math.sin(dlat / 2)
				+ Math.cos(a_x_point * Math.PI / 180)
				* Math.cos(b_x_point * Math.PI / 180) * Math.sin(dlon / 2)
				* Math.sin(dlon / 2);
		Double cDouble = 2 * Math.atan2(Math.sqrt(aDouble), Math
				.sqrt(1 - aDouble));
		long d = Math.round((R * cDouble) * 1000) / 1000;
		return d;

	}

相关推荐

Global site tag (gtag.js) - Google Analytics