Location Clustering Strategies in Rails

I’m working on a Rails application that stores location data inside one of its models then provides that data to an iOS app for display. I’m able to successfully cluster on the device, but that’s not a scaleable solution.

I’m going to move the clustering to the server and was wondering if anyone has enough experience that they might be able to suggest an approach.

In my naive approach I would define a Cluster object that has an associated zoom level, a centroid, and a child count. I’d use something to recalculate the centroid when new children are added. This would allow me to request Clusters where zoom_level is X and centroid is within a lat/lon region. I think that my naive approach could work, but it could grow to be a slow process and runs the risk of becoming out of date if many location objects are inserted at once.

I’m wondering if there might be some Postgres functionality that would calculate clusters on the fly or an optimized approach to clustering.