8

I have a map-based application, using Google Maps' iOS SDK. I need to store up to several thousand items in a core data database and display them with markers on the map. For performance and usability reasons, I need to cluster these markers when the user is zoomed out, but I need to make sure to place representative markers so the user knows where to zoom in to see more detail.

Each entry in my core data model has latitude/longitude double values stored. So what I thought of for clustering the items is to keep a separate entity where I strip the less significant parts of the geographic coordinates and store a count in it.

So whenever an item with lat/lon {44.9382719, -130.20293849} is inserted in the database, another "cluster" object with lat/lon {44.9, -130.2} has its count property incremented. The idea is that at low zooms (ie. zoomed out), I would only query the cluster objects and place those on the map instead of the actual items.

My question is: according to the NSManagedObject reference, you're not supposed to fetch stuff in awakeFromInsert, so how can I make sure that inserting a managed object of one kind updates the value of a corresponding managed object of another kind?

Shinigami
  • 2,123
  • 23
  • 40
  • 1
    Actually, i take reference from https://github.com/romaonthego/REMarkerClusterer – Horst Sep 10 '13 at 03:26
  • That's interesting, but it looks like it uses MapKit. Will it work with Google Maps too? – Shinigami Sep 10 '13 at 03:39
  • You need to modify the marker and cluster's delegate method – Horst Sep 10 '13 at 03:54
  • 1
    possible duplicate of [Marker clustering with google maps SDK for iOS?](http://stackoverflow.com/questions/20175605/marker-clustering-with-google-maps-sdk-for-ios) – Andrei Radulescu May 12 '14 at 13:01
  • Have you managed to do this? I am working on a similar project, but in swift and I am having trouble – Andrei Dobrin May 05 '15 at 09:42
  • I don't think I found a good solution at the time: I would break the visible region into a grid and in each cell in the grid, if the number of markers was higher than a threshold, I would place a "group" marker on the map. It didn't look good because clustering shouldn't happen on predefined grid points. Rather it should be based on marker density in the region. – Shinigami May 11 '15 at 18:52

2 Answers2

8

I have been searching library for Clustering Markers in Google Maps for iOS for three days, and finally I ended up with this https://github.com/googlemaps/google-maps-ios-utils, which is working nicely and very easy to use and understand.

Nitesh Borad
  • 4,583
  • 36
  • 51
  • The code on the link has a code for some Quad Tree and nothing Google-Maps related. can you elaborate how to use it? – Dvole Aug 20 '14 at 14:11
  • 2
    It seems that author of https://github.com/googlemaps/google-maps-ios-utils has removed some files from its library... Although you can get its previous files which were there, when i downloaded from it, which contains Clustering Algorithm and iOS Example Project, from the following link http://goo.gl/R0wgeh – Nitesh Borad Aug 21 '14 at 09:01
  • Thanks @NiteshBorad ... I've got this library to work for me successfully. However I was wondering if there's a way to get the marker objects inside a cluster? e.g. if I tap on a cluster, I wan to show a list of markers in it. Is it possible? – Hyder Sep 10 '15 at 05:43
  • 1
    @Sha: Yes, you can do that. Add custom GSMMarker (so that cluster can distinguish from default marker) at cluster position coordinate. Make your view controller delegate for GSMMapView and implement GMSMapViewDelegate protocol's method "- mapView:didTapMarker:" in your view controller. Now, when user clicks on our custom cluster marker, this method will be called. On call, zoom out the map view which will show normal location points in map, at which you can add default GSMMarker. – Nitesh Borad Jun 14 '16 at 14:11
1

Have a look at routeMe, this comes default

/** Whether the annotation should be clustered when map view clustering is enabled. Defaults toYES. */ @property (nonatomic, assign) BOOL clusteringEnabled;

CocoaChris
  • 508
  • 3
  • 7
  • 1
    Huh... I was wondering why all the [MapBox SDK](https://www.mapbox.com/mapbox-ios-sdk/) classes started in 'RM'. Now it makes sense. I did end up implementing MapBox on that project, which offers its own clustering as you pointed out. But its map quality isn't anywhere close to Google Maps, and that one seems to have no nice way of clustering the markers. I did manually write clustering code for Google Maps but it doesn't animate nicely. – Shinigami Nov 14 '13 at 19:24
  • Do you have the clustering code you wrote for the Google Maps SDK? I'm in a similar position. If you have something open source, I can maybe help out and work on the animation stuff. Easier to spend my time on that than writing my own. – Bill Burgess Dec 10 '13 at 22:03