mapbox-maps-ios icon indicating copy to clipboard operation
mapbox-maps-ios copied to clipboard

Clustering

Open JenishKananiLime opened this issue 3 years ago • 1 comments

New Feature

Is there a way to cluster pointannotation manager instead of using symbol layers?

for use case I see point annotation manager has better performance then rendering with symbol layers but would also like to cluster in point annotations. <- Description of the feature being requested and any outcomes desired, an example use case, and any suggestions for solution ->

Why

<- 1-2 sentence description of why you're requesting this feature. What problem does it solve? Why is it valuable? ->

JenishKananiLime avatar Aug 16 '22 14:08 JenishKananiLime

Please note that this isn't necessarily a supported use case and will work depending on our particular implementation of the annotation manager, which is subject to change. Cluster is an immutable property so it can only be set when the source is created.

The source and layer re-creation either:

  • has to happen before the first time the annotation manager syncs its annotations to the style, or
  • needs to be followed by a force-sync

Please see the example below:

final class DebugViewController: UIViewController {

    var mapView: MapView!

    override func viewDidLoad() {
        super.viewDidLoad()
        mapView = MapView(frame: view.bounds)
        mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        view.addSubview(mapView)

        DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
            self.go()
        }
    }

    func go() {
        let mgr = mapView.annotations.makeCircleAnnotationManager()

        mgr.annotations = (0...100).map { _ in
            var anno = CircleAnnotation(centerCoordinate: CLLocationCoordinate2D(
                latitude: .random(in: -0.1...0.1),
                longitude: .random(in: -0.1...0.1)))
            anno.circleColor = StyleColor(.red)
            return anno
        }

        try! mapView.mapboxMap.style.removeLayer(withId: mgr.layerId)
        try! mapView.mapboxMap.style.removeSource(withId: mgr.sourceId)

        // Add the source with empty `data` property
        var source = GeoJSONSource()
        source.data = .empty
        source.cluster = true
        source.clusterRadius = 100
        try! mapView.mapboxMap.style.addSource(source, id: mgr.sourceId)

        var layer = CircleLayer(id: mgr.layerId)
        layer.source = mgr.sourceId
        try! mapView.mapboxMap.style.addPersistentLayer(layer)
    }
}

ZiZasaurus avatar Aug 17 '22 13:08 ZiZasaurus