Advertise here

Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Update MKAnnotation Coordinates without knowing variable name?

ChrisYatesChrisYates Posts: 370Registered Users @ @
I appreciate the question might seem odd but basically I am adding annotations to the map after pulling coordinates from a backend database. The amount of annotations being added vary depending on the user.
let details = Annotation(title: "\(userName)",
                                         locationName: "",
                                         coordinate: CLLocationCoordinate2D(latitude:convertLat!, longitude: convertlon!))

                self.mapView.addAnnotation(details as MKAnnotation)
                self.mapView.selectAnnotation(details, animated: true)

The trouble I am having is I would like to update the coordinates of the 'details' annotation at specific intervals but I am unable to access the 'details' annotation because of course it is out of scope.

Is it possible, for example, to access an annotation through its title name and change its coordinates accordingly?

The other alternative is to remove all annotations and recreate them with the updated coordinates but this is something I am trying to avoid.

It's probably worth noting that for a number of reasons I can't just simply create the details annotation outside off my methods.

I did try the following approach which was to grab all the maps annotations and to try and to search by title and then update the title but it's a get only property!
for annotation in mapView.annotations as [MKAnnotation] {

        if (annotation as AnyObject).title == "Title of annotation"   {

           annotation.title = "Change to something else"

<a href=""; target="_blank">Velvet Rock Apps</a><br/>
Sign In or Register to comment.