首页 > 解决方案 > 注释引脚颜色和类型

问题描述

我的应用程序具有向地图添加注释的方法

 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 = []
   }

我的注释是

import UIKit
import MapKit

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

根据specialty1类型,我想要不同的引脚颜色(和类型),我正在尝试使用下面的方法。停止显示的一件事是 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。

我错过了什么?有没有办法做我需要的?

谢谢

标签: iosswiftannotationsmapkit

解决方案


MKPinAnnotationView不会在图钉下方显示名称。它仅在您点击它并显示标注时显示。

如果你想要这种风格的圆形注释视图,下面有名称,应该使用MKMarkerAnnotationView. 为了改变它的颜色,你设置markerTintColor.


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

我建议在以下位置注册一个重用 ID viewDidLoad

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

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

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

当您可以开始重用注释视图时,这会更有效。


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

例如,我将子类化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。如果您有多个自定义注释重用标识符,您只需要这样做。


推荐阅读