Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Realm for filtering and sorting nearby locations

I want to query a realm db for nearby locations. The latitude and longitude informations are saved in the database. When I generate a bounding box, then I can just query all stations within the bounding box. I know that it isn't a radius, because I'm using a bounding box.

So I have a RealmResult with all locations in the area. I want to keep it as a RealmResult to benefit from the auto updating objects.

How can I sort the locations by distance to my current location? Only the locations and latitudes of the location are persisted and sortable.

Additionally it would be great if I could add an additional filter for the distance, so all locations are within the radius.

like image 221
vork Avatar asked Dec 07 '25 06:12

vork


1 Answers

Realm currently doesn't support geospatial queries, but we have an issue tracking it here: https://github.com/realm/realm-java/issues/1772

So I am afraid you are a bit out of luck doing it through the Realm API, but you might consider looking into something like GeoHash: https://en.wikipedia.org/wiki/Geohash. It is a representation of location using a bitstring and distance can be approximated comparing prefixes.

like image 74
Christian Melchior Avatar answered Dec 09 '25 20:12

Christian Melchior



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!