首页 > 解决方案 > iOS - 选择/取消选择 Apple 地图中的标记不起作用

问题描述

我在地图上选择多个标记。
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView){} 标记选择工作正常,并且正在调用地图委托方法。

必需的:

当标注显示时,我将取消选择标注显示需要取消选择的同一个标记。

使用堆栈

 

class ArtworkView: MKAnnotationView {
var locItem:LocationItem = LocationItem()


override var annotation: MKAnnotation? {
  willSet {
    guard let artwork = newValue as? Artwork else {return}
    locItem = artwork.locItem
    if(!artwork.isUser){
        canShowCallout = true
        calloutOffset = CGPoint(x: -5, y: 5)
        rightCalloutAccessoryView = nil
        if let imageName = artwork.imageName {
            image = UIImage(named: imageName)
        } else {
            image = nil
        }

        detailCalloutAccessoryView = detailLabel
    }else{
        canShowCallout = false
        calloutOffset = CGPoint(x: -5, y: 5)
        rightCalloutAccessoryView = nil
        image = UIImage(named: "gifcurrentloc")
        detailCalloutAccessoryView = nil
    }

  }
}

标签: iosmapkitmkmapviewmkannotationmkannotationview

解决方案


showCallout()/hideCallout()中, selectAnnotation:/deselectAnnotation:通过地图使用。

showCalloutView:/中,hideCalloutView:不要调用setSelected:. MKAnnotationView

您不应直接调用此方法。MKMapView对象调用此方法以响应用户与注释的交互。


推荐阅读