根据经纬度周边范围搜索


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

*/
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

* @date 2013年8月28日下午10:36:04

* @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 getInRangeDriver(final double fromLat, final double fromLng) {
final List list = new ArrayList();
final Iterator 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 list = getInRangeDriver(104.068699,30.629431);
if (list != null)
for (DriverHeartbeatEntity d : list)
System.out.println(d.getUsername());
}
}

你可能感兴趣的:(工具类)