java附近的人_java实现按范围搜索附近人(Jfinal框架)

Jfinal框架实现按范围搜索附近人,并按距离升序排序,实例代码:

公共类Localtion:

/**

* 生成以中心点为中心的四方形经纬度

*

* @param lat 经度

* @param lon 纬度

* @param raidus 半径(以千米为单位)

* @return

*/

public static double[] getAround(double lat, double lon, int raidus) {

Double latitude = lat;

Double longitude = lon;

Double degree = (24901 * 1609) / 360.0;

double raidusMile = raidus;

Double dpmLat = 1 / degree;

Double radiusLat = dpmLat * raidusMile*1000; //千米,去掉*1000就是米

Double minLat = latitude - radiusLat;

Double maxLat = latitude + radiusLat;

Double mpdLng = degree * Math.cos(latitude * (Math.PI / 180));

Double dpmLng = 1 / mpdLng;

Double radiusLng = dpmLng * raidusMile*1000;  //千米,去掉*1000就是米

Double minLng = longitude - radiusLng;

Double maxLng = longitude + radiusLng;

return new double[] { minLat, minLng, maxLat, maxLng };

}

/**

* 计算中心经纬度与目标经纬度的距离(米)

*

* @param centerLon

*            中心精度

* @param centerLan

*            中心纬度

* @param targetLon

*            需要计算的精度

* @param targetLan

*            需要计算的纬度

* @return 米

*/

public static double distance(double centerLon, double centerLat, double targetLon, double targetLat) {

double jl_jd = 102834.74258026089786013677476285;// 每经度单位米;

double jl_wd = 111712.69150641055729984301412873;// 每纬度单位米;

double b = Math.abs((centerLat - targetLat) * jl_jd);

double a = Math.abs((centerLon - targetLon) * jl_wd);

return Math.sqrt((a * a + b * b));

}

Controller类方法:public void doGetQuserNearList() {

String range=getPara("range");//搜索范围  这里以千米为单位

if (!BaseConverter.isFull(range)) {

range ="1";

}

String lng=getPara("lng");

String lat=getPara("lat");

Page page =null;

List param = new ArrayList();

StringBuffer where = new StringBuffer();

double[] lalon=Location.getAround(Double.valueOf(lat), Double.valueOf(lng), Integer.valueOf(range));

if (BaseConverter.isFull(lng)) {

where.append(" and a.LONGITUDE >= ? and a.LONGITUDE <= ?");

param.add(lalon[1]);

param.add(lalon[3]);

System.out.println(">="+lalon[1]);

System.out.println("<="+lalon[3]);

}

if (BaseConverter.isFull(lat)) {

where.append(" and a.latitude >= ? and a.latitude <= ?");

param.add(lalon[0]);

param.add(lalon[2]);

System.out.println(">="+lalon[1]);

System.out.println("<="+lalon[2]);

}

//按距离升序排序

String orderby =" order by distance asc   ";

page = Db.use("oracle").paginate(pageIndex, pageSize,"select b.dyxm,c.U_ID,c.LATITUDE,c.LONGITUDE,c.DATE_TIME,c.DYZH," +

" trunc(ACOS(SIN(('"+lat+"' * 3.1415) / 180 ) * SIN((c.latitude * 3.1415) / 180 )+COS(('"+lat+"' * 3.1415) / 180 )*COS((c.latitude * 3.1415) / 180 )*COS(('"+lng+"' * 3.1415) / 180 - (c.LONGITUDE * 3.1415) / 180 ) ) * 6380,3) distance",

"  from T_TEM_GPRS c,G_BASICINFO b " +

" where  c.dyzh = b.dyzh and c.DATE_TIME=( select max(DATE_TIME) from T_TEM_GPRS a where " +

"   a.DYZH=c.DYZH and to_date(DATE_TIME,'yyyy-mm-dd,hh24:mi:ss') >= SYSDATE-1/24" + //一个小时之内

where.toString()+ ")  "  + orderby, param.toArray() );

renderJson(page);

}本文出自http://swzhinan.com/

本文由站长原创或收集,不代表本站立场,如若转载,请注明出处:http://www.swzhinan.com/post/154.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值