Este artigo apresenta o código detalhado de Java para converter o GeoHash em coordenadas correspondentes de latitude e longitude. É compartilhado com você para sua referência. O conteúdo específico é o seguinte
pacote com.lulei.geo; importar java.util.arraylist; importar java.util.arrays; importar java.util.hashmap; importar java.util.list; importação com.lulei.geo.bean.LocationBean; importação com.lulei.util.jsonutil; classe pública Geohash {Localização Private Location; /** * 1 2500 km; 2 630 km; 3 78km; 4 30km * 5 2,4 km; 6 610m; 7 76m; 8 19m */ private int hashlength = 8; // latitude e longitude são convertidas em comprimento geohash privado int latlength = 20; // latitude e longitude são convertidas em comprimento binário privado int lngle comprimento = 20; // Latitude e longitude são convertidos em minlat duplo privado de comprimento binário; // tamanho unitário de cada latitude private duplo minlng; //Unit size of each latitude 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'}; Hashmap estático privado <personagem, número inteiro> charsmap; static {charsmap = new Hashmap <caractere, 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: Defina a unidade mínima de latitude e longitude* / private void setMinlatlng () {minlat = locationBean.maxlat - locationBean.Minlat; for (int i = 0; i <comprimento latl; i ++) {minlat /= 2.0; } minlng = localBean.maxlng - locationBean.Minlng; for (int i = 0; i <lnglength; i ++) {minlng /= 2.0; }} / ** * @return * @Author: lulei * @Description: Encontre os nove pontos de coordenadas e pontos circundantes * / lista pública <String> getGeoHashBase32For9 () {duplo leftLat = location.getLat () - minlat; dupla direitaLAT = Location.getLat () + minlat; duplo uplng = local.getlng () - minlng; duplo downlng = local.getlng () + minlng; List <String> base32for9 = new ArrayList <String> (); // as três cordas à esquerda de cima para baixo para esquerda = getGeoHashBase32 (LeftLat, Uplng); if (! (leftup == null || "" .equals (leftup))) {base32for9.add (leftup); } String leftMid = getGeoHashBase32 (leftlat, location.getlng ()); if (! (LeftMid == null || "" .equals (esquerdmid))) {base32for9.add (esquerda); } String leftDown = getGeoHashBase32 (Leftlat, Downlng); if (! (LeftDown == null || "" .equals (leftdown))) {base32for9.add (leftdown); } // As 3 cordas de cima para baixo no meio -médio = 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 strings de cima para baixo no lado direito da direita = 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); } retornar base32for9; } / ** * @param Comprimento * @return * @Author: lulei * @Description: Defina a latitude e a longitude para o comprimento geohash * / public boolean sethashlength (int length) {if (comprimento <1) {return false; } hashLength = length; Latlength = (comprimento * 5) / 2; if (comprimento % 2 == 0) {lnglesten = Latlength; } else {lnglength = Latlength + 1; } setMinlatlng (); retornar true; } / ** * @return * @Author: lulei * @description: obtenha a sequência base32 de latitude e longitude * / public String getGeohashbase32 () {return getGeohashbase32 (location.getlat (), location.getlng ()); } / ** * @param lat * @param lng * @return * @author: lulei * @description: obtenha a sequência base32 de latitude e longitude * / private string getGeohashbase32 (duplo lat, duplo 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 = novo booleano [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: converta binário de cinco bits em base32 * / private char getBase32char (boolean [] base32) {if (base32 == null || base32.Length! = 5) {return ''; } int num = 0; for (boolean bool: base32) {num << = 1; if (bool) {num += 1; }} retorna chars [num % chars.length]; } / ** * @param i * @return * @author: lulei * @description: convert os números em string binária * / private string getBase32BinaryString (int i) {if (i <0 || i> 31) {return null; } String str = Integer.TobinaryString (i + 32); retornar str.substring (1); } / ** * @param geohash * @return * @author: lulei * @description: converta geohash em string binária * / private string getGeohashbinaryString (string geohash) {if (geohash == 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: retorne as coordenadas correspondentes a geohash * / public locationBean 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); duplo lngValue = getGeohashmid (lng.tostring (), locationBean.Minlng, locationBean.maxlng); LocalBean Location = New LocationBean (LatValue, LngValue); location.setGeohash (Geohash); Localização de retorno; } / ** * @param binarystr * @param min * @param max * @return * @author: lulei * @description: retorne o valor intermediário correspondente ao binário * / privado duplo getGeohashmid (string binarys, duplo min, duplo max) {if (binarys == null | } 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: obtenha a sequência binária geográfica de coordenadas * / private boolean [] getGeobiny (Lat Double, Lng duplo) {boolean [] Latarray = Gethasharray (LatBean.min.Mina; boolean [] lngarray = gethasharray (lng, locationbean.Minlng, locationBean.maxlng, lngle comprimento); Merge de retorno (Latarray, Lngarray); } / ** * @param latRray * @param lngarray * @return * @author: lulei * @description: mescle latitude e longitude binária * / private boolean [] mescle (boolean [] latArray, boolean [] lngarray) {if (latarray == null | } boolean [] resultado = new boolean [lngarray.length + latarray.length]; Arrays.fill (resultado, falso); for (int i = 0; i <lngarray.length; i ++) {resultado [2 * i] = lngarray [i]; } para (int i = 0; i <latRray.length; i ++) {resultado [2 * i+1] = latRray [i]; } resultado de retorno; } / ** * @param valor * @param min * @param max * @return * @author: lulei * @description: convert os números em string binária geohash * / private boolean [] gethasharray (valor duplo, duplo min, duplo max, int) {if (valor <min | } if (comprimento <1) {return null; } boolean [] resultado = novo booleano [comprimento]; for (int i = 0; i <comprimento; i ++) {duplo mid = (min+max) / 2.0; if (value> mid) {resultado [i] = true; min = médio; } else {resultado [i] = false; max = médio; }} Retornar resultado; } public static void main (string [] args) {// TODO Method Auto-Generated Stub Geohash G = New Geohash (40.221227, 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 (a DOZINGIL.GetDistance (bean.getlat (), bean.getlng (), bean.getlat (), bean.getlat () - g.minlat, bean.getlng () - g.minlng)); }}O exposto acima é o conteúdo detalhado deste artigo, espero que seja útil para o aprendizado de todos.