在MapView(Swift)中单击MKPlaceMark时,该如何调用功能



我尝试在我的mapview上单击mkplacemark时尝试执行某个功能。此功能从placemark中提取信息,并在单独的视图上显示。我试图使用

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {}

但这似乎不起作用。我不确定是否必须命名每个placemark,我之所以避免这样做,是因为我已经在名称中显示了位置数据(可以在我的代码中看到)。任何帮助将非常感激。我的代码:

启动placemark(这发生了多次,变量已经实例化):

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)

您需要在viewDidLoad

内设置委托
self.mapView.delegate = self

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
  guard let ann = view.annotation as? MKPlacemark else { return }
  print(ann)
}

最新更新