Yandex MapKit iOS:集群和地标

huangapple go评论92阅读模式
英文:

Yandex MapKit iOS : cluster and placemarks

问题

我正在使用Yandex Mapkit iOS SDK进行我的一个项目。

看来SDK允许将地标添加到一个集群中。但是我无法像添加一个mapObject一样添加一个带有userData的自定义地标。我想要检测标记上的点击操作。

// 将标记添加为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"))
// 直到调用这个方法之前,标记才会显示出来。它必须在集合更改后强制调用以更新集群
collection.clusterPlacemarks(withClusterRadius: 20, minZoom: 5)
英文:

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)

答案1

得分: 3

定义一个带有监听器的集合。用任意点填充数组。遍历数组并将每个点添加到集合中。当将点添加到集合时,返回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 points: [YMKPoint] = [] // 用任意点填充数组

    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 = "用户数据"
        }

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

请注意,上述代码是一个Swift类的示例,用于在地图上添加点并进行聚类。如果需要更多帮助或有其他问题,请随时提问。

英文:

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

huangapple
  • 本文由 发表于 2020年1月6日 23:35:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/59614848.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定