【问题标题】:Annotation Pin color and Type注释引脚颜色和类型
【发布时间】:2019-11-29 00:30:56
【问题描述】:

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

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

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

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

谢谢

【问题讨论】:

    标签: ios swift annotations mapkit


    【解决方案1】:

    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,并将注解视图的配置放在它所属的注解视图类中。 (这就是我们使用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。仅当您有多个自定义注释重用标识符时才需要这样做。

    【讨论】:

    • 嗨@Rob.. 非常感谢.. 我听从了你的建议并删除了 viewFor
    • 嗨@Rob,我在markerTintColor下方添加了另一段代码,以根据缩放对标记进行聚类,但它不起作用.. let annotationView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: nil) annotationView.clusteringIdentifier = annotation.specialty1 如果我打印 Specialty,我看到有内容,但是,即使关闭,当我缩小时,它也会从屏幕上消失而不是集群。你知道为什么吗?谢谢
    • 用“clusteringIdentifier = annotation.especialidade1”替换该代码,它正在工作。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多