Geohash-Java Search Nearby LatLongs

可紊 提交于 2019-12-08 08:19:30

问题


I've been searching everywhere for a clear example of how to use the ch.hsr.geohash to search for nearby locations. I mean, I just need to implement the following situation:

1 - I have a Long latitude and Long longitude. 2 - Convert this latitude/longitude to hash. (I believe it's done by using "GeoHash.withBitPrecision(latitude, longitude, 64)" but I'm not sure what precision is here. I suppose 64bits would be to have the same precision as long 3 - Retrieve a list of latitudes/longitudes within 100km distance (I have no idea how to even start it using geohash) 4 - Query objectify objects using the latitudes/longitudes result list.

But I can't find any documentation about the lib neither any clear example. Please could someone advice me or give me any start point to search about it? Did anyone already used this library to achieve the same result I'm looking for? Thanks a lot!


回答1:


I ended with the following approach:

Geohash geohash = Geohash. withCharacterPrecision(latitude, longitude,12); String geohashString = geohash.toBase32();

Then worked with geohashString according to the distance I wanted. I mean, matching the prefix of the string according to its precision. For example,

Database:

Name | geohash | id

Model 1 | gc7x9813vx2r | 1
Model 2 | gc7x8840suhr | 2
Model 3 | gc30psvp0zgr | 3

Then I want to get all models within 100km radius of this point (53.244664, -6.140530).

Geohash geohash = Geohash. withCharacterPrecision(53.244664, -6.140530, 12);
String geohashString = geohash.toBase32().substring(0, 3); //3 characters for around 100km of precision

ofy().load().type(Model.class).filter("geohash >=", geoHashString).filter("geohash <", geoHashString + "\uFFFD");

Then it will match only Model 1 and Model 2 which start with "gc7" so they are within 100km radius approximately.

Following this table for precision: https://en.wikipedia.org/wiki/Geohash

I actually could simplify the steps to:

String geohashString = Geohash. withCharacterPrecision(53.244664, -6.140530, 3).toBase32();

I didn't do any performance test yet though but I don't think it'll get much slower. Anyway, if the performance test "fails" I will implement the same but using binaryString instead.



来源:https://stackoverflow.com/questions/45091236/geohash-java-search-nearby-latlongs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!