条件:先知道该区块geohash值或者该区块任意一点gps
推荐测试工具:geohash在线
geohash工具类
public class GeoHash {
private LocationBean location;
/**
* 1 2500km;2 630km;3 78km;4 20km
* 5 2.4km; 6 610m; 7 76m; 8 19m
*/
private int hashLength = 7; //经纬度转化为geohash长度
private int latLength = 20; //纬度转化为二进制长度
private int lngLength = 20; //经度转化为二进制长度
private double minLat;//每格纬度的单位大小
private double minLng;//每个经度的单位大小
private static final char[] CHARS = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n',
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
private static HashMap<Character, Integer> CHARSMAP;
static {
CHARSMAP = new HashMap<Character, Integer>();
for (int i = 0; i < CHARS.length; i++) {
CHARSMAP.put(CHARS[i], i);
}
}
public GeoHash(double lat, double lng) {
location = new LocationBean(lat, lng);
setMinLatLng();
}
public GeoHash() {
super();
}
public int gethashLength() {
return hashLength;
}
/**
*
* @Description: 设置经纬度的最小单位
*/
private void setMinLatLng() {
minLat = LocationBean.MAXLAT - LocationBean.MINLAT;
for (int i = 0; i < latLength; i++) {
minLat /= 2.0;
}
minLng = LocationBean.MAXLNG - LocationBean.MINLNG;
for (int i = 0; i < lngLength; i++) {
minLng /= 2.0;
}
}
/**
* @return
* @Description: 求所在坐标点及周围点组成的九个
*/
public List<String> getGeoHashBase32For9() {
double leftLat = location.getLat() - minLat;
double rightLat = location.getLat() + minLat;
double upLng = location.getLng() - minLng;
double downLng = location.getLng() + minLng;
List<String> base32For9 = new ArrayList<String>();
//左侧从上到下 3个
String leftUp = getGeoHashBase32(leftLat, upLng);
if (!(leftUp == null || "".equals(leftUp))) {
base32For9.add(leftUp);
}
String leftMid = getGeoHashBase32(leftLat, location.getLng());
if (!(leftMid == null || "".equals(leftMid))) {
base32For9.add(leftMid);
}
String leftDown = getGeoHashBase32(leftLat, downLng);
if (!(leftDown == null || "".equals(leftDown))) {
base32For9.add(leftDown);
}
//中间从上到下 3个
String midUp = getGeoHashBase32(location.getLat(), upLng);
if (!(midUp == null || "".equals(midUp))) {
base32For9.add(midUp);
}
String midMid = getGeoHashBase32(location.getLat(), location.getLng());
if (!(midMid == null || "".equals(midMid))) {
base32For9.add(midMid);
}
String midDown = getGeoHashBase32(location.getLat(), downLng);
if (!(midDown == null || "".equals(midDown))) {
base32For9.add(midDown);
}
//右侧从上到下 3个
String rightUp = getGeoHashBase32(rightLat, upLng);
if (!(rightUp == null || "".equals(rightUp))) {
base32For9.add(rightUp);
}
String rightMid = getGeoHashBase32(rightLat, location