ios - 注释引脚颜色和类型
问题描述
我的应用程序具有向地图添加注释的方法
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。
我错过了什么?有没有办法做我需要的?
谢谢
解决方案
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
。如果您有多个自定义注释重用标识符,您只需要这样做。
推荐阅读
- javascript - Javascript:如何删除嵌入式删除按钮的父列表项
- java - 如何在 Java 中从用户那里获取多行输入,然后将输入存储在字符串类型的数组中,其中每一行都是数组的一个元素
- sql - 在这个 SQL 作业中遇到一些问题
- php - 无法将获取的行项目保存到会话中
- javascript - 更新 !!Angular 10:向特定模块注入服务,检测到循环依赖。@inject 和提供者[]之间的区别?
- c# - 使用 IEnumerable 或特定类型返回
- php - 表中15条记录的最后一次查询,但包含相关表中的数据
- azure - 无法从 Azure 函数连接到 Azure SQL
- r - 如何在R中使用二元运算符而不覆盖
- tcl - 在 TCL 中声明常量的方法是什么?