Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you want to update the properties of an annotation that has already been added to the map, adding it again with the new properties (without first removing the old one) just creates and adds <em>another</em> annotation at the same location (with the new properties).</p> <p>As you saw, calling <code>removeAnnotation</code> and then <code>addAnnotation</code> results in flicker and another drop animation.</p> <p>Instead, you have to get a reference to the <em>existing</em> annotation and then update its properties.</p> <p>If it's just one annotation, you could keep a class-level ivar reference to it and update the properties with that reference when the values change.</p> <p>If you need to update different annotations at different times, a simple way is to search the map view's <code>annotations</code> array for the annotation you want to update. </p> <p>This requires that you have a property in your annotation class that will be unique for each annotation and (ideally) remain constant for each annotation. In other words: if you're going to be updating the annotation's <code>title</code>, don't use the <code>title</code> as the "unique" annotation identifier. </p> <p>Instead, add another property (eg. an int or string) that you assign to each annotation when creating it and which won't change so you can find the annotation later using that value.</p> <p>For example, assume you add an int property called <code>annotationId</code> to your annotation class and you want to update the annotation with id# 42:</p> <pre><code>BOOL annFound = NO; //loop through the map view's annotations array to find annotation id# 42... for (id&lt;MKAnnotation&gt; ann in mapView.annotations) { if ([ann isKindOfClass:[MyAnnotationClass class]]) { MyAnnotationClass *myAnn = (MyAnnotationClass *)ann; if (myAnn.annotationId == 42) { annFound = YES; myAnn.title = @"some new title"; break; } } } if (!annFound) { //annotation id# 42 is not yet on the map. //create it and add to map... } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload