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()
==
does not mean what you think it does. These are Double values; you cannot reliably compare them using==
. – matttrunc(annotLatitude)
is what he's talking about – djv