开发者

How do I update an MKAnnotation location with new coordinates?

i'm having problems trying to update an annotation location with different coordinates. Is there any way I can change de location property without having to create another开发者_如何学编程 annotation? I've tried the code below with no luck. The annotation i'm trying to get is not updating its location. Please help!

CLLocationCoordinate2D location;
location.latitude = -36.560976;
location.longitude = -59.455807; 

for (id annotation in self.mapView.annotations) {

    if ([annotation isKindOfClass:[MyAnnotation class]]) 
    {
            [annotation setCoords:location]; //setCoords is defined in MyAnnotation class
            }
    }

Thank you all!


Extend MKPointAnnotation, it has the capability to automatically update the annotation view on the map to the new coordinates.

@interface MyAnnotation : MKPointAnnotation


You need to remove the annotation and add it again after changing its coordinates, or replace it with a new one. MapKit does not assume annotations might be mutable.


You should not have to remove your annotation and then re-add it in order to update the annotation's location on the map.

The MapView that you have added your annotation to should take care of location changes for you. Without seeing more code, I think the problem you are having might be that your annotation uses a location variable rather than a coordinate variable.

The MKAnnotation protocol requires that you implement a CLLocationCoordinate2D coordinate instance variable in your annotation class. It also recommends that you synthesize a coordinate property.

Try adding the CLLocationCoordinate2D coordinate instance variable and its matching property @property (assign, nonatomic) CLLocationCoordinate2D coordinate to your annotation class .h file. Then use a @synthesize in your annotation class .m file.

If this doesn't solve your problem you might have to add more detail to your question.


How to move a MKAnnotation without adding/removing it from the map?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜