gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 15:29:03 25 4
gpt4 key购买 nike

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

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

但我的问题是,当生成此数组时,顺序是随机的(MKUserLocation 有时在数组中排在第一位,有时排在第二位)。我想知道是否有一种方法能够获取(在本例中)Swift.ColorPointAnnotation,以便我能够使用此注释来操作 map 。

下面是我如何操作数组。我希望能够获取特定注释,以便将 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: { $0 is ColorPointAnnotation }) {

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

}

关于ios - Swift 3.0 操作和调用 MKAnnotation 数组中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44915744/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com