根据经纬度周边范围搜索


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import cn.quinn.dabei.cache.HeartBeatFactory;
import cn.quinn.dabei.model.business.DriverHeartbeatEntity;

/**
*
*
* @date 2013年8月18日下午2:50:09<br>
*/
public class MapUtils {


public static final int ONE_LNG = 0x1b2d7;

public final static int ONE_LAT = 0xe31f;

public static double[] countCoordinates(double dist) {
double l[] = new double[2];
l[0] = 1d / ONE_LNG * dist;
l[1] = 1d / ONE_LAT * dist;
return l;
}

/**
*
* @param lng1
* 经度
* @param lat1
* 纬度
* @param lng2
* @param lat2
* @return<br>
* @date 2013年8月28日下午10:36:04<br>
* @author
*/
public static double distanceByLngLat(double lng1, double lat1, double lng2, double lat2) {
double radLat1 = lat1 * Math.PI / 180;
double radLat2 = lat2 * Math.PI / 180;
double a = radLat1 - radLat2;
double b = lng1 * Math.PI / 180 - lng2 * Math.PI / 180;
double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
s = s * 6378137.0;
s = Math.round(s * 10000) / 10000;
return s;
}

public static synchronized List<DriverHeartbeatEntity> getInRangeDriver(final double fromLat, final double fromLng) {
final List<DriverHeartbeatEntity> list = new ArrayList<DriverHeartbeatEntity>();
final Iterator<DriverHeartbeatEntity> drivers = HeartBeatFactory.getInstance().getDriver().values().iterator();
while (drivers.hasNext()) {
final DriverHeartbeatEntity driver = drivers.next();
if (driver.getStatus() == DriverHeartbeatEntity.STATUS_OFF_WORK)
continue;
final int distance = driver.getDistance();
final double dou[] = MapUtils.countCoordinates(distance);
final double lat = driver.getLatitude();
final double lng = driver.getLongitude();
final double bigLat = lat + dou[1];
final double smlLat = lat - dou[1];
final double bigLng = lng + dou[0];
final double smlLng = lng - dou[0];
if ((fromLat <= bigLat && smlLat <= fromLat) && (fromLng <= bigLng && fromLng >= smlLng))
list.add(driver);
}
return list;
}

public static void main(String[] args) {
DriverHeartbeatEntity d1 = new DriverHeartbeatEntity();
d1.setCreateTime(System.currentTimeMillis());
d1.setLatitude(104.068791);
d1.setLongitude(30.62948);
d1.setUsername("a1");
d1.setDistance(3000);
HeartBeatFactory.getInstance().putDriver(d1);
List<DriverHeartbeatEntity> list = getInRangeDriver(104.068699,30.629431);
if (list != null)
for (DriverHeartbeatEntity d : list)
System.out.println(d.getUsername());
}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值