Search code examples
androidlocationrealm

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.


Solution

  • 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.