Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clustering in MapKit with Swift

Tags:

swift

mapkit

Is there in Swift the possibility to cluster markers in MapKit? I know that it's possible in Google Maps SDK, but I can't find the same feature in MapKit. I've found some libraries for Obj-C, but nothing for Swift

These some of the libraries that I have found:

  • FBAnnotationClustering
  • CCHMapClusterController
like image 378
RiccardoCh Avatar asked Jun 23 '15 08:06

RiccardoCh


People also ask

What is MapKit in Swift?

Display map or satellite imagery within your app, call out points of interest, and determine placemark information for map coordinates.

What's new in MapKit?

MapKit has supported overlays with several styling options for years. In iOS 16, we are improving our existing APIs to allow your overlays to seamlessly integrate with the map. Let's start with a quick recap of overlay levels. Overlays can be rendered at two different levels: above roads and above labels.


2 Answers

I translated FBAnnotationClustering to Swift:

https://github.com/ribl/FBAnnotationClusteringSwift

Works with MapKit. It's a working sample project, and the readme includes some integration and usage instructions.

There's also an associated blog post giving background on why we chose FBAnnotationClustering (I liked it), and why we translated it to Swift (didn't feel like troubleshooting Objective-C if we ran into issues later).

http://ribl.co/blog/2015/05/28/map-clustering-with-swift-how-we-implemented-it-into-the-ribl-ios-app/

like image 66
Robert Chen Avatar answered Nov 13 '22 20:11

Robert Chen


Use MKMarkerAnnotationViews for easy clustering. Here's an example of class I have, where you can set the clustering identifier.

Annotations with the same clustering identifier will automatically cluster.

class AnnotationView: MKMarkerAnnotationView {

    override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
        super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)


        self.clusteringIdentifier = "AnnotationViewIdentifier"
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}
like image 20
Zach DeGeorge Avatar answered Nov 13 '22 20:11

Zach DeGeorge