Swift 3.0 操作和调用 MKAnnotation 数组中的元素
Swift 3.0 Manipulate and Call Upon Elements in MKAnnotation Array
我想知道如何操作由 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)
您可以这样做以从该数组中获取第一个 ColorPointAnnotation
。
if let pin = mapView.annotations.first(where: { [=10=] is ColorPointAnnotation }) {
mapView.setRegion(MKCoordinateRegionMakeWithDistance(pin.coordinate, 500, 500), animated: true)
}
我想知道如何操作由 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)
您可以这样做以从该数组中获取第一个 ColorPointAnnotation
。
if let pin = mapView.annotations.first(where: { [=10=] is ColorPointAnnotation }) {
mapView.setRegion(MKCoordinateRegionMakeWithDistance(pin.coordinate, 500, 500), animated: true)
}