我想知道如何在点击时快速从注释中获取所有信息。 有这篇文章: Swift, How to get information from a custom annotation on clicked
但这不能回答我的问题。我对代码做了一些修改。
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
if let annotationTitle = view.annotation?.title {
if let annotationPhone = view.annotation?.phoneNumber as? MKAnnotationView {
print("User tapped on annotation with title: \(annotationPhone!)")
}
print("User tapped on annotation with title: \(annotationTitle!)")
}
}
创建图钉(如果有帮助的话)
let annotation = MKPointAnnotation()
annotation.coordinate = item.placemark.coordinate
annotation.title = item.name
annotation.subtitle = "Coffee Shop"
self.map.addAnnotation(annotation)
但是我得到了错误:
Value of type 'MKAnnotation' has no member 'phoneNumber'
我在做什么错了?
答案 0 :(得分:0)
只是名为MKPointAnnotation
的属性的子类phoneNumber
:
class PhoneAnnotation : MKPointAnnotation {
var phoneNumber: String?
}
之后,您可以像这样从注释中获取phoneNumber
值(和其他自定义值):
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
if let phoneNumber = (view.annotation as? PhoneAnnotation)?.phoneNumber {
}
}