Get all groups within coordinates

I'm currently creating Elgg instance which will start with around 300k groups. I'd like to view all those groups within a map (leafletjs, but that isn't relevant). Loading 300k of groups in a single AJAX request (on a mobile device) is a lot, so I'd like to only return the visible groups on the map's square.

After some searching I came across this question, which seems to be really straightforward. Except, I'm currently stuck on implementing this into Elgg. This because Elgg uses multiple tables for metadata. 

Do you guys have any idea how this could be implemented and how to start? I've added a `X` and `Y` metadata to some (not all, those without doesn't need to appear on the map) ElggGroup objects. 

I'm currently trying multiple ways, but none of them really seems to work with the current structure (multiple joins) of the database.