【发布时间】: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