I'm trying to mark a location in a map view.
First I implemented the
MKAnnotation protocol in a separate class like this.
@interface AddressAnnotation : NSObject <MKAnnotation>
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
self = [super init];
self.coordinate = location;
Then in the view controller, I implemented the
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
if ([annotation isKindOfClass:[MKUserLocation class]])
static NSString *myIdentifier = @"myIndentifier";
MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:myIdentifier];
pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:myIdentifier];
pinView.pinColor = MKPinAnnotationColorRed;
pinView.animatesDrop = NO;
And in the
viewDidLoad method, I initialize an instance of the
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(34.421496, -119.70182);
AddressAnnotation *pinAnnotation = [[AddressAnnotation alloc] initWithCoordinates:coordinate];
I keep getting the following error though.
-[AddressAnnotation setCoordinate:]: unrecognized selector sent to instance
I'm not sure what I'm doing wrong here. Can someone please help me out?
The problem is that you defined a readonly coordinate property. Therefore you get an exception when trying to set it. Just remove your coordinate property definition, as this is already provided by the MKAnnotation protocol.
Your problem is that the
coordinate property is readonly. Moreover, it's already defined by the
When you implement a protocol defines @properties you actually have to @syntentize those properties to create a backing instance variable (ivar) in your class or, alternatively, you got to implement custom setter and getter for those properties.
Remember: a property is nothing more that a setter and getter method and can be optionally backed by an ivar. Being backed by an ivar is a @property's default behaviour.
So, in short, to fix your problem in your
AddressAnnotation.h file delete the
coordinate property and in the
AddressAnnotion.m file add:
// after the line @implementation Address Annotation @syntetize coordinate = _coordinate;