Yandex MapKit iOS:集群和地标
Posted
技术标签:
【中文标题】Yandex MapKit iOS:集群和地标【英文标题】:Yandex MapKit iOS : cluster and placemarks 【发布时间】: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。这个库简化了我的工作并解决了与从模型中挖掘和传输数据到用户的定义相关的问题。 【参考方案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)
【讨论】:
以上是关于Yandex MapKit iOS:集群和地标的主要内容,如果未能解决你的问题,请参考以下文章
如何在 yandex mapkit 中获取我当前位置的坐标?
Yandex MapKit 3.0。获取屏幕和缩放的中心,在地图上获取位置