【问题标题】:Swift 3.0 Manipulate and Call Upon Elements in MKAnnotation ArraySwift 3.0 操作和调用 MKAnnotation 数组中的元素
【发布时间】:2017-12-08 12:05:02
【问题描述】:

我想知道如何操作由 MKAnnotations 组成的数组。这个数组的一个例子是

[<MKUserLocation: 0x608000631500>, <Swift.ColorPointAnnotation: 0x600000861e00>]

但我的问题是,当这个数组生成时,顺序是随机的(MKUserLocation 有时在数组中是第一个,其他时候是第二个)。我想知道是否有一种方法可以抓取,在这种情况下,Swift.ColorPointAnnotation 以便我能够使用此注释操作地图。

这是我操作数组的方式。我希望能够获取特定的注释,以便将 mapRegion 围绕它居中。

let annotations = self.mapView.annotations
let coordinateRegion = MKCoordinateRegionMakeWithDistance((annotations.last?.coordinate)!, 500, 500)
mapView.setRegion(coordinateRegion, animated: true) 

【问题讨论】:

    标签: ios swift dictionary annotations mkmapview


    【解决方案1】:

    您可以这样做以从该数组中获取第一个 ColorPointAnnotation

    if let pin = mapView.annotations.first(where: { $0 is ColorPointAnnotation }) {
    
        mapView.setRegion(MKCoordinateRegionMakeWithDistance(pin.coordinate, 500, 500), animated: true)
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-16
      • 2020-08-21
      • 2017-02-26
      • 2012-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多