The goal is to customize the pin colors per some values stored in an structure array.
Per some help here I implemented the the following viewForAnnotation delegate m
First, the code should not be calling viewForAnnotation
explicitly itself.
Remove the explicit call to viewForAnnotation
after the addAnnotation
line.
viewForAnnotation
is a delegate method and the map view will call it automatically when it needs to display an annotation. If it's not getting called automatically, make sure the map view's delegate
property is set (to self
for example).
Second (and the real issue), is that the code assumes that the viewForAnnotation
delegate method will get called only once and immediately after adding each annotation.
This is not the case and is not guaranteed. The map view will call viewForAnnotation
whenever it needs to display the annotation and could be called multiple times for the same annotation or long after the annotation is actually added (eg. after user pans or zooms the map and the annotation comes into view).
See does MKAnnotationView buffer its input queue? for some additional details and relevant links to other answers including sample code.
Basically, you must store the properties that affect an annotation's view with the annotation object itself and retrieve these properties from the annotation
parameter that is passed into viewForAnnotation
.
What I suggest for your case is this:
I assume you are using the built-in annotation class MKPointAnnotation
. Instead of using MKPointAnnotation
which does not let you store your custom status
property alongwith the annotation object itself, either:
Create a custom class that implements the MKAnnotation
protocol but also with a status
property. Set this property when creating the annotation and extract its value from the annotation
parameter passed into viewForAnnotation
and set the pinColor
accordingly. See sample code in linked answer(s).
Make the objects in the MySupplierData
themselves objects that implement the MKAnnotation
protocol. So if the objects in MySupplierData
are instances of some class named, say, Supplier
, make the Supplier
class conform to the MKAnnotation
protocol and then you can add the MySupplierData
objects themselves to the map view when calling addAnnotation
.