If you update the annotation's coordinate using a setCoordinate method (or equivalent), the map view will automatically update the position of the annotation on the view. This page in the docs says the following:
Important: When you implement the
coordinate property in your class, it
is recommended that you synthesize its
creation. If you choose to implement
the methods for this property
yourself, or if you manually modify
the variable underlying that property
in other parts of your class after the
annotation has been added to the map,
be sure to send out key-value
observing (KVO) notifications when you
do. Map Kit uses KVO notifications to
detect changes to the coordinate,
title, and subtitle properties of your
annotations and make any needed
changes to the map display. If you do
not send out KVO notifications, the
position of your annotations may not
be updated properly on the map.
The map view will only know to re-read the coordinate property of the annotation if it's told (via KVO) that the coordinate has changed. One way to do that is implement a setCoordinate method and call that wherever you have the code that updates the annotation's location.
In your code, you are re-calculating the coordinate in the readonly coordinate property itself. What you could do is add this to the annotation .m file (and to the .h):
- (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate
{
//do nothing
}
and in the place where you update locations, call the setCoordinate method on the annotation:
[someAnnotation setCoordinate:someAnnotation.coordinate];
You could do this in the place where you currently remove/re-add the annotations.
The above call looks funny because you have the coordinate re-calc in the coordinate-getter method. Although it should work as a quick fix/test, I don't recommend using it regularly.
Instead, you could re-calc the annotation's location outside (where you currently remove/re-add the annotations) and pass the new coordinate to setCoordinate. Your annotation object could store its new location in the lat/lng ivars you currently have (set them in the setCoordinate and use only those to construct a CLLocationCoordinate2D to return from the getter) or (better) use the coordinate ivar itself (set it in setCoordinate and return it in the getter).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…