I am trying to add different images to different annotation views, in other words, i want a unique pic to correspond to each unique pin. Here is what I am trying:
That line gives an error because CLLocationCoordinate2D
is a type of struct and theCoordinate1
is, I assume, a variable of type CLLocationCoordinate2D
. You can't compare the two.
What you are trying to do is compare the coordinates of the current annotation for which a view is being requested to the coordinates in theCoordinate1
. To do that, if you must, you need something like this:
if ((annotation.coordinate.latitude == theCoordinate1.latitude)
&& (annotation.coordinate.longitude == theCoordinate1.longitude)) {
However, I don't recommend comparing floating point numbers that way even if it "works" sometimes. If you must compare coordinates, use CLLocation's distanceFromLocation:
method and see if the distance between the two is below some threshold like 10.0 meters.
Another way to check if the annotation is the one you're looking for is to keep a reference to the annotation itself (the one you passed to the addAnnotation:
method) and then you can do if (annotation == theAnnotation1)
.
If you don't want to keep a reference to the annotations, you can also check if the title of the annotation is the one your're looking for (if ([annotation.title isEqualToString:@"Jeff"])
).
The best way is to add a custom property (ideally an int) to a custom annotation class and check for that in viewForAnnotation.
A few other unrelated things:
calloutAccessoryControlTapped
delegate method which will give a reference to the annotation (see How to find which annotation send showDetails? for example).dequeueReusableAnnotationViewWithIdentifier
in viewForAnnotation to re-use views (see EXC_BAD_ACCESS with MKPinAnnotationView for example).