serdar aylanc
serdar aylanc

Reputation: 1347

Yandex MapKit iOS : cluster and placemarks

I am using Yandex Mapkit iOS SDK for one of my projects.

It seems that SDK allows adding placemarks is a cluster. Bu I can not add a custom place mark with userData as the same way adding a placemark as a mapObject. I want to detect tap action on a marker.

// adding markers as mapobjects:
let point = YMKPoint(coordinate: CLLocationCoordinate2D(latitude: Double(hit.geom!.lat ?? 0), longitude: Double(hit.geom?.lon ?? 0)))
let placemark: YMKPlacemarkMapObject
self.mapObjects = self.mapView.mapWindow.map.mapObjects
            
placemark = mapObjects!.addPlacemark(with: point, image: #imageLiteral(resourceName: "marker"))
placemark.userData = MarkerUserData(id: Int(hit.id!)!, description: hit.plate!)
placemark.isDraggable = false
placemark.addTapListener(with: self)
            
mapObjects!.addListener(with: self)

Adding markers in a cluster, markers can be added to a cluster using only YMKPoint. I could not find a way to add a placemark object inside a cluster

let point = YMKPoint(coordinate: CLLocationCoordinate2D(latitude: Double(hit.geom!.lat ?? 0), longitude: Double(hit.geom?.lon ?? 0)))
let placemark: YMKPlacemarkMapObject
        
collection.addPlacemark(with: point, image: #imageLiteral(resourceName: "marker"))
// Placemarks won't be displayed until this method is called. It must be also called
// to force clusters update after collection change
collection.clusterPlacemarks(withClusterRadius: 20, minZoom: 5)

Upvotes: 2

Views: 4220

Answers (1)

Belzik
Belzik

Reputation: 519

Define a collection with a listener. Fill the array with any points. Go through the array and add each point to the collection. When adding a point to the collection, YMKPlacemarkMapObject is returned, adding user data. And extend your controller delegate method.

And look at the test project with Yandex - https://github.com/yandex/mapkit-ios-demo/blob/master/MapKitDemo/ClusteringViewController.swift

class MapViewController: UIViewController {
    @IBOutlet weak var mapView: YMKMapView!
    var collection: YMKClusterizedPlacemarkCollection?    
    var point: [YMKPoint] = [] // Fill the array with any points

    override func viewDidLoad() {
        super.viewDidLoad()

        collection = mapView.mapWindow.map.mapObjects.addClusterizedPlacemarkCollection(with: self)
        collection?.addTapListener(with: self)

        for point in points {
            let placemark = collection?.addPlacemark(with: point,
                                                           image: UIImage(named: "some_image")!,
                                                           style: YMKIconStyle.init())
            placemark?.userData = "user data"
        }

        collection.clusterPlacemarks(withClusterRadius: 60, minZoom: 15)
    }
}

extension MapViewController: YMKMapObjectTapListener {
    func onMapObjectTap(with mapObject: YMKMapObject, point: YMKPoint) -> Bool {
        guard let userPoint = mapObject as? YMKPlacemarkMapObject else {
            return true
        }

        print(userPoint.userData)
    }
}

Upvotes: 3

Related Questions