首页 > 解决方案 > 是否有预设的“索引”或类似的东西可以让我引用用户创建的特定注释?斯威夫特 4

问题描述

我有一个应用程序,用户可以在其中搜索位置,点击表格视图中的行,然后在地标处放置注释。我有一个按钮“meetUpButton”,它允许他们放置多个注释,而不是我的代码默认执行的操作,即在单击新搜索结果时删除注释。有没有办法引用我的用户创建的特定注释,即使他们做了多个?例如,假设我想添加我的用户创建的两个注释的纬度......如果我有

let annotation = MKPointAnnotation()
annotation.coordinate.latitude

有没有办法引用索引为 0 的第一个注释和我的用户用 1 或其他方式选择的下一个注释?这是我的一些代码,可能会使这更清楚。

extension ViewController: handleMapSearch {
func dropPinZoomIn(placemark:MKPlacemark) {
    resultSearchController?.isActive = false

    // cache the pin
    selectedPin = placemark
    // clear existing pins
    if meetUpOutlet.isEnabled == true {

        mapView.removeAnnotations(mapView.annotations)

    } else {

    }

    let annotation = MKPointAnnotation()

    annotation.coordinate = placemark.coordinate
    annotation.title = placemark.name
    if let city = placemark.locality,
        let state = placemark.administrativeArea {
        annotation.subtitle = "(\(city)) (\(state))"
    }
    mapView.addAnnotation(annotation)
    let span = MKCoordinateSpanMake(0.05, 0.05)
    let region = MKCoordinateRegionMake(placemark.coordinate, span)
    mapView.setRegion(region, animated: true)
}
} 

标签: iosswiftxcodeannotationsmapkit

解决方案


您可以尝试使用实例annotations的属性MKMapView,以获得所有使用

let anns = self.mapView.annotations 

推荐阅读