Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone MKMapView Annotation Clustering

I've got quite a lot of pins to put on my map so I think it would be a nice idea to cluster those annotations. I'm not really sure how to achieve this on iPhone, I was able to work something out with google maps and some javascript examples. But iPhone uses its mkmapview and I have no idea how to cluster annotations in there.

Any ideas or frameworks that you know and are good? Thanks.

like image 962
haluzak Avatar asked Oct 18 '11 00:10

haluzak


People also ask

What is annotation clustering?

Allow your users to cluster annotations for better visualization. By default, annotations clustering is disabled. You can decide per each marker if it should be included in the clustering process or not (e.g., you want to have a few annotations that are never clustered).

What is MapKit in iOS?

MapKit is a powerful API available on iOS devices that makes it easy to display maps, mark locations, enhance with custom data and even draw routes or other shapes on top.


2 Answers

You don't necessarily need to use a 3rd party framework because since iOS 4.2, MKMapView has a method called - (NSSet *)annotationsInMapRect:(MKMapRect)mapRect which you can use to do your clustering.

Check out the WWDC11 Session video 'Visualizing Information Geographically with MapKit'. About half way through it explains how to do it. But I'll summarize the concept for you:

  • Use Two maps (second map is never added to the view hierarchy)
  • Second map contains all annotations (again, it's never drawn)
  • Divide map area into a grid of squares
  • Use -annotationsInMapRect method to get annotation data from invisible map
  • Visible map builds its annotations from this data from invisible map

enter image description here

like image 90
eric Avatar answered Sep 20 '22 14:09

eric


Fortunately, you don't need 3rd party framework's anymore. iOS 11 has native clustering support.

You need to implement mapView:clusterAnnotationForMemberAnnotations: method.

Get more details in the Apple example: https://developer.apple.com/sample-code/wwdc/2017/MapKit-Sample.zip

like image 40
ricardopereira Avatar answered Sep 19 '22 14:09

ricardopereira