This article describes the Java implementation of the Geohash into the corresponding latitude and longitude of the coordinates of the detailed code, to share with you for your reference, the specific content as follows
Package Com.lulei.geo;
Import java.util.ArrayList;
Import Java.util.Arrays;
Import Java.util.HashMap;
Import java.util.List;
Import Com.lulei.geo.bean.LocationBean;
Import Com.lulei.util.JsonUtil;
public class Geohash {private Locationbean location; /** * 1 2500km;2 630km;3 78km;4 30km * 5 2.4km; 6 610m; 7 76m; 8 19m */private int hashlength = 8; Transformed by latitude into geohash length private int latlength = 20; The latitude is converted into binary length private int lnglength = 20; Convert longitude to binary length private double minlat;//per unit size of latitude private double minlng;//per longitude unit size private static final char[] Char S = {' 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 int gethashlength () {return hashlength; /** * @Author: Lulei * @Description: Set the minimum unit of latitude/longitude/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 * @Author: Lulei * @Description: Nine/public list<string> of the coordinates point and the surrounding point are obtained Getgeo
Hashbase32for9 () {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> ();
Left from top to bottom 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);
}//middle from top to bottom 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);
///right from top to bottom 3 String rightup = GetGeoHashBase32 (Rightlat, UPLNG); if (!) ( Rightup = = NULL | |
". Equals (Rightup))) {Base32for9.add (rightup); } String rightmid = GetGeoHashBase32 (riGhtlat, LOCATION.GETLNG ()); if (!) ( Rightmid = = NULL | |
". Equals (Rightmid))) {Base32for9.add (rightmid);
String Rightdown = getGeoHashBase32 (Rightlat, DOWNLNG); if (!) ( Rightdown = = NULL | |
". Equals (Rightdown))) {Base32for9.add (Rightdown);
return BASE32FOR9; /** * @param length * @return * @Author: Lulei * @Description: Set latitude and longitude into geohash length * * Public boolean S
Ethashlength (int length) {if (length < 1) {return false;
} hashlength = length;
Latlength = (length * 5)/2;
if (length% 2 = 0) {lnglength = Latlength;
else {lnglength = latlength + 1;
} setminlatlng ();
return true;
/** * @return * @Author: Lulei * @Description: Obtain longitude and latitude Base32 string/public string getGeoHashBase32 () {
Return GetGeoHashBase32 (Location.getlat (), LOCATION.GETLNG ()); /** * @param lat * @param LNG * @return * @Author: Lulei * @Description: Obtain longitude and latitude base32 String * * priv Ate string getGeoHashBase32 (double lat, double LNG) {boolean[] Bools = getgeobinary (lat, LNG);
if (bools = = null) {return null;
StringBuffer sb = new StringBuffer ();
for (int i = 0; i < bools.length i = i + 5) {boolean[] base32 = new BOOLEAN[5];
for (int j = 0; J < 5; J +) {Base32[j] = bools[i + j];
} Char cha = Getbase32char (base32);
if (' = = Cha ') {return null;
Sb.append (CHA);
return sb.tostring (); /** * @param base32 * @return * @Author: Lulei * @Description: Convert five-bit binary to base32/private char Getb
Ase32char (boolean[] base32) {if (Base32 = = NULL | | base32.length!= 5) {return ';
int num = 0;
For (Boolean bool:base32) {num <<= 1;
if (bool) {num = 1;
} return chars[num% chars.length]; /** * @param i * @return * @Author: Lulei * @Description: Converts a number to a binary string/private string getbase32b inarystring (int i) {if(I < 0 | | | i > 31)
{return null;
String str = integer.tobinarystring (i + 32);
return str.substring (1); /** * @param geohash * @return * @Author: Lulei * @Description: Convert Geohash to binary string/private string Getgeohashbinarystring (String geohash) {if Geohash = null | |
"". Equals (Geohash)) {return null;
StringBuffer sb = new StringBuffer ();
for (int i = 0; i < geohash.length (); i++) {char c = geohash.charat (i);
if (Charsmap.containskey (c)) {String cStr = getbase32binarystring (Charsmap.get (c));
if (CStr!= null) {sb.append (CStr);
}} return sb.tostring (); /** * @param geohash * @return * @Author: Lulei * @Description: return geohash corresponding coordinates */public LOCATIONB
Ean getLocation (String geohash) {string geohashbinarystr = getgeohashbinarystring (Geohash);
if (geohashbinarystr = = null) {return null;
StringBuffer lat = new StringBuffer (); StringbufFer lng = new StringBuffer (); for (int i = 0; i < geohashbinarystr.length (); i++) {if (i% 2!= 0) {lat.append (Geohashbinarystr.charat (i))
;
else {lng.append (Geohashbinarystr.charat (i));
} Double latvalue = Getgeohashmid (lat.tostring (), Locationbean.minlat, Locationbean.maxlat);
Double lngvalue = Getgeohashmid (lng.tostring (), LOCATIONBEAN.MINLNG, LOCATIONBEAN.MAXLNG);
Locationbean location = new Locationbean (Latvalue, lngvalue);
Location.setgeohash (Geohash);
return location; /** * @param binarystr * @param min * @param max * @return * @Author: Lulei * @Description: Return binary pair Median value/private double Getgeohashmid (String binarystr, Double min, double max) {if (Binarystr = null | | binarys
Tr.length () < 1) {return (min + max)/2.0;
} if (Binarystr.charat (0) = = ' 1 ') {return Getgeohashmid (binarystr.substring (1), (min + max)/2.0, Max); else {return Getgeohashmid (Binarystr.substriNg (1), Min, (min + max)/2.0); }/** * @param lat * @param LNG * @return * @Author: Lulei * @Description: Get the coordinates of the GEO binary string * * p Rivate boolean[] getgeobinary (double lat, double LNG) {boolean[] Latarray = Gethasharray (lat, Locationbean.minlat, Loc
Ationbean.maxlat, latlength);
boolean[] Lngarray = Gethasharray (LNG, LOCATIONBEAN.MINLNG, LOCATIONBEAN.MAXLNG, lnglength);
Return merge (Latarray, Lngarray); /** * @param latarray * @param lngarray * @return * @Author: Lulei * @Description: Combined longitude and latitude binary * * p Rivate boolean[] Merge (boolean[] Latarray, boolean[] lngarray) {if (Latarray = null | | lngarray = NULL) {return
Null
Boolean[] result = new Boolean[lngarray.length + latarray.length];
Arrays.fill (result, false);
for (int i = 0; i < lngarray.length i++) {result[2 * i] = lngarray[i];
for (int i = 0; i < latarray.length i++) {result[2 * i + 1] = Latarray[i];
return result; /** * @param value * @param min * @param max * @return * @Author: Lulei * @Description: Convert numbers to Geo Hash binary String */private boolean[] Gethasharray (double value, double min, double max, int length) {if (Value < mi n | |
Value > Max) {return null;
} if (length < 1) {return null;
} boolean[] result = new Boolean[length];
for (int i = 0; i < length; i++) {Double mid = (min + max)/2.0;
if (Value > mid) {result[i] = true;
Min = mid;
else {Result[i] = false;
max = mid;
} return result; public static void Main (string[] args) {//TODO auto-generated method stub Geohash g = New Geohash (40.2212
27, 116.24875);
String Geohash = G.getgeohashbase32 ();
System.out.println (Geohash);
Locationbean bean = g.getlocation (Geohash);
System.out.println (Jsonutil.parsejson (bean));
System.out.println (New Geohash (Bean.getlat (), BEAN.GETLNG ()). GETGEOHASHBASE32 ());System.out.println (Distanceutil.getdistance (Bean.getlat (), BEAN.GETLNG (), Bean.getlat ()-G.minlat, BEAN.GETLNG ()-
G.MINLNG));
}
}
The above is the detailed content of this article, I hope to help you learn.