注释引脚颜色和类型

Annotation Pin color and Type

我的应用程序有一个方法可以向地图添加注释

 var annotationArray = [MyAnnotation]()
 var allAnnotations: [(objLat: CLLocationDegrees, objLong: CLLocationDegrees, objName: String, objDesc: String, objId: String)] = []

  func addAnnotationsToMap() {
    annotationArray = []
        for oneObject in self.allAnnotations {
            let oneAnnotation = MyAnnotation()
            let oneObjLoc: CLLocationCoordinate2D = CLLocationCoordinate2DMake(oneObject.objLat, oneObject.objLong)
            oneAnnotation.coordinate = oneObjLoc
            oneAnnotation.title = oneObject.objName
            oneAnnotation.subtitle = oneObject.objDesc
            oneAnnotation.mapId = oneObject.objId

            self.annotationArray.append(oneAnnotation)
        }
        self.map.addAnnotations(self.annotationArray)
        self.allAnnotations = []
        self.annotationArray = []
   }

MyAnnotation 是

import UIKit
import MapKit

class MyAnnotation: MKPointAnnotation {
    var mapId = String()
    var phone1 = String()
    var phone2 = String()
    var specialty1 = String()
    var specialty2 = String()
}

根据 specialty1 类型,我想要不同的 Pin 颜色(和类型),我正在尝试使用下面的方法。停止显示的一件事是 Pin 图标题很难(这是我想保留的)

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    let annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: nil)

    annotationView.pinTintColor = UIColor.red

    return annotationView
}

我的想法是使用下面不同颜色的 Pin 图

如果我删除 viewFor Annotation,我会得到一个带有标题的 Pin 图。

有几篇文章展示了如何向注释添加不同的颜色,但他们都使用 addAnnotation 而不是 addAnnotations。

我错过了什么?有什么办法可以满足我的需求吗?

谢谢

MKPinAnnotationView 没有在图钉下显示名称。它仅在您点击它和显示标注时显示。

如果您想要这种带有名称的圆形注释视图样式,应该使用 MKMarkerAnnotationView。要更改其颜色,请设置 markerTintColor.


顺便说一下,我不建议像那样直接实例化您的注释视图。

我建议在 viewDidLoad 中注册一个重用 ID:

mapView.register(MKMarkerAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)

然后,在 viewFor 中,您可以像这样使可重用注释视图出列:

let view = mapView.dequeueReusableAnnotationView(withIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier, for: annotation) as! MKMarkerAnnotationView
view.markerTintColor = ...

当您可以开始重用注释视图时,效率会更高..


现在,我会更进一步,完全删除 viewFor,并将注释视图的配置放在它所属的注释视图 class 中。 (这就是我们使用 MKMapViewDefaultAnnotationViewReuseIdentifier 的原因。)

例如,我将子class MKMarkerAnnotationView:

class MyAnnotationView: MKMarkerAnnotationView {
    override var annotation: MKAnnotation? { didSet { update(for: annotation) } }

    override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
        super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
        update(for: annotation)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}


private extension MyAnnotationView {
    func update(for annotation: MKAnnotation?) {
        guard let annotation = annotation as? MyAnnotation else { return }

        markerTintColor = ...
    }
}

然后在 viewDidLoad 中注册此注释视图:

mapView.register(MyAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)

但是在这个模式中,你根本没有实现 viewFor。如果您有多个自定义注释重用标识符,您只需要这样做。