Question

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?

Was it helpful?

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.

OTHER TIPS

If you don't specifically need to use an MKPlacemark (it doesn't look like it because you're passing nil for the addressDictionary), you could use the MKPointAnnotation class instead.

MKPointAnnotation implements MKAnnotation but adds a setCoordinate method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top