首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

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

2012-12-24 
java根据经纬度计算两点距离(java 代码 sql 代码)项目上正好用到 。顺便贴给JE们。public double distance(d

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());}
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;}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  处理呢 。   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 吧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 吧
解?double jl_jd : 102834.74258026089786013677476285 米; double jl_wd : 111712.69150641055729984301412873 米; double jl_jd : 102834.74258026089786013677476285 米; double jl_wd : 111712.69150641055729984301412873 米;


单位米

热点排行