objective-cmkmapviewmkannotationmkplacemark

calling setCoordinate on MKPlacemark crashes app with 'unrecognized selector'


I have a MKMapView i'm adding a single MKPlacemark to to represent the location of a building the user has just selected. The user can only select one building at a time, and I simply want to move the placemark to the new building when they select a new building. On the first building they select, it works fine and places a pin on the map. When I try and call setCoordinate on the placemark to update the position of the marker when they select a new building though, I get -[MKPlacemark setCoordinate:]: unrecognized selector sent to instance

In MyViewController.h I have:

@property (nonatomic, strong)MKPlacemark *selectedBuildingPlacemark;

In MyViewController.m

@synthesize selectedBuildingPlacemark;

...

if (self.selectedBuildingPlacemark == nil) {
        self.selectedBuildingPlacemark = [[MKPlacemark alloc] initWithCoordinate:myCoord addressDictionary:nil];
        [mapView addAnnotation:self.selectedBuildingPlacemark];
    }
    else {
        [self.selectedBuildingPlacemark setCoordinate:myCoord];
    }

I thought MKPlacemark conformed to MKAnnotation and should therefore implement setCoordinate. Can someone show me the error of my ways?


Solution

  • The documentation of MKAnnotation says:

    Annotations that support dragging should implement this method to update the position of the annotation.

    So the method setCoordinate: is optional and is only implemented by classes that support dragging. The documentation of MKPlacemark does not reference that method, so it is not implemented.

    So you should create a new instance every time you select a new building.