每当我在MapView上单击MKPlacemark时,我都试图执行某些功能。此功能从地标中提取信息并将其显示在单独的视图中。我尝试使用
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {}
但是这似乎不起作用。我不确定是否必须为每个地标命名,因为我已经在名称中显示了位置数据(如我的代码所示),因此避免了这样做。任何帮助将非常感激。我的代码:
初始化地标(这种情况已经发生了很多次,变量已经被实例化了):
let coords = CLLocationCoordinate2DMake(latOfPlace!, lonOfPlace!)
let address = [CNPostalAddressStreetKey: addressStreetKey, CNPostalAddressCityKey: addressCityKey, CNPostalAddressPostalCodeKey: addressPostalCodeKey, CNPostalAddressISOCountryCodeKey: addressISOCountryCodeKey]
let place = MKPlacemark(coordinate: coords, addressDictionary: address)
self.mapView.addAnnotation(place)
答案 0 :(得分:1)
您需要将委托设置在viewDidLoad
self.mapView.delegate = self
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
guard let ann = view.annotation as? MKPlacemark else { return }
print(ann)
}