Geohash-Java Search Nearby LatLongs

做~自己de王妃 提交于 2019-12-07 02:26:26

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.

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