Swift 3.0操作和调用MKAnnotation数组中的元素

时间:2017-07-05 01:15:55

标签: ios swift dictionary annotations mkmapview

我想知道我是如何操作由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) 

1 个答案:

答案 0 :(得分:1)

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

if let pin = mapView.annotations.first(where: { $0 is ColorPointAnnotation }) {

    mapView.setRegion(MKCoordinateRegionMakeWithDistance(pin.coordinate, 500, 500), animated: true)

}