1
votes

When I add an annotation on my map, I want to highlighted it.
I know I must use mapView1.selectAnnotation(annotation: MKAnnotation!, animated: Bool)

But I can't get my MKAnnotation object, all I have is the location (CLLocationCoordinate2D) of the annotation.

My question is How I can get the specific Annotation knowing his location ?

EDIT : Solution found in theory but had a bug : for annotation in self.mapView1.annotations as! [MKAnnotation] { println(" ") let annotLatitude = annotation.coordinate.latitude let annotLongitude = annotation.coordinate.longitude let eventLatitude = self.appDel!.lastEventCoords.latitude let eventLongitude = self.appDel!.lastEventCoords.longitude println("annotLatitude : (annotLatitude) , annotLongitude : (annotLongitude)") println("eventLatitude : (eventLatitude) , eventLongitude : (eventLongitude)")
if (annotLatitude == eventLatitude && annotLongitude == eventLongitude) { self.lastAnnotationAdded = annotation println("Location equals") self.mapView1.selectAnnotation(self.lastAnnotationAdded!, animated: true) break }

Output : annotLatitude : 47.4799380142289 , annotLongitude : -0.44762351737708 eventLatitude : 47.4799380142289 , eventLongitude : -0.44762351737708

The latitude and longitude are exactly the same but the "if" loop is not executed ! Sometimes it works but sometimes not. HOW is it possible ???

EDIT2 : Problem solved
To compare the coordinates, I had to truncate them before with the func tronc()

1
Because == does not mean what you think it does. These are Double values; you cannot reliably compare them using ==.matt
For anyone searching: trunc(annotLatitude) is what he's talking aboutdjv

1 Answers

1
votes

The MKMapView has a list of its annotations, its annotations property. So look through those until you find the one you want (the one with the right coordinate).