Kevin
Kevin

Reputation: 1189

Swift 3.0 Manipulate and Call Upon Elements in MKAnnotation Array

I am wondering how I am able to manipulate an array consisting of MKAnnotations. An example of this array is

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

but my issue is that when this array is generated, the order is randomized (MKUserLocation is first in the array sometimes, second other times). I was wondering if there was a way to be able to grab, in this case, Swift.ColorPointAnnotation such that I am able to manipulate the map with this annotation.

Here is how I have been manipulating the array. I wish to be able to grab the specific annotation in order to center the mapRegion around it.

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

Upvotes: 0

Views: 70

Answers (1)

Tristan Beaton
Tristan Beaton

Reputation: 1762

You could do this to get the first ColorPointAnnotation from that array.

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

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

}

Upvotes: 1

Related Questions