我想知道如何在点击时从swift中的注释中获取所有信息。这里有一篇文章: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'
我做错了什么?
发布于 2018-07-04 05:44:50
只需使用名为phoneNumber
的属性子类MKPointAnnotation
即可:
class PhoneAnnotation : MKPointAnnotation {
var phoneNumber: String?
}
在此之后,您可以从注释中获得phoneNumber
值(和其他定制值),如下所示:
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
if let phoneNumber = (view.annotation as? PhoneAnnotation)?.phoneNumber {
}
}
https://stackoverflow.com/questions/51161435
复制相似问题