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;}