【问题标题】:Yandex MapKit iOS : cluster and placemarksYandex MapKit iOS:集群和地标
【发布时间】:2020-01-06 15:35:44
【问题描述】:

我正在为我的一个项目使用 Yandex Mapkit iOS SDK

看来SDK允许添加地标是一个集群。但是我不能像添加地标为mapObject 一样使用userData 添加自定义地标。我想检测标记上的点击动作。

// 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)

在集群中添加标记,可以仅使用YMKPoint 将标记添加到集群中。我找不到在集群内添加placemark 对象的方法

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)

【问题讨论】:

  • 仅供参考:大量使用集群和地图,我可以推荐支持 Yandex Mapkit 的 an excellent library。这个库简化了我的工作并解决了与从模型中挖掘和传输数据到用户的定义相关的问题。

标签: ios swift yandex-mapkit


【解决方案1】:

定义一个带有监听器的集合。用任意点填充数组。遍历数组并将每个点添加到集合中。向集合添加点时,返回 YMKPlacemarkMapObject,添加用户数据。并扩展您的控制器委托方法。

然后用 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)
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-04
    • 2019-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-06
    相关资源
    最近更新 更多