0

I'm trying to mark a location in a map view.

First I implemented the MKAnnotation protocol in a separate class like this.

AddressAnnotation.h

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface AddressAnnotation : NSObject <MKAnnotation>

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

- (id)initWithCoordinates:(CLLocationCoordinate2D)location;

@end

AddressAnnotation.m

#import "AddressAnnotation.h"

@implementation AddressAnnotation

- (id)initWithCoordinates:(CLLocationCoordinate2D)location
{
    self = [super init];
    if (self)
    {
        self.coordinate = location;
    }

    return self;
}

@end

Then in the view controller, I implemented the MKMapViewDelegate.

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MKUserLocation class]])
    {
        return nil;
    }

    static NSString *myIdentifier = @"myIndentifier";
    MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:myIdentifier];

    if (!pinView)
    {
        pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:myIdentifier];
        pinView.pinColor = MKPinAnnotationColorRed;
        pinView.animatesDrop = NO;
    }
    return pinView;
}

And in the viewDidLoad method, I initialize an instance of the AddressAnnotation class.

- (void)viewDidLoad
{
    [super viewDidLoad];

    CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(34.421496, -119.70182);

    AddressAnnotation *pinAnnotation = [[AddressAnnotation alloc] initWithCoordinates:coordinate];
    [self.mapView addAnnotation:pinAnnotation];
}

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?

Thank you.

Isuru
  • 30,617
  • 60
  • 187
  • 303

2 Answers2

1

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.

rist
  • 578
  • 2
  • 9
  • I got it working. Thanks. But the region shown in the screen isn't the marked area. How do I focus on the region where the pin is when the app starts? – Isuru Jan 25 '14 at 15:55
  • MKCoordinateRegion mapRegion; mapRegion.center = coordinate; mapRegion.span.latitudeDelta = 0.05; mapRegion.span.longitudeDelta = 0.05; [mapView setRegion:region animated:YES] – Zeb Jan 25 '14 at 16:17
0

Your problem is that the coordinate property is readonly. Moreover, it's already defined by the MKAnnotation protocol.

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;
Gianluca Tranchedone
  • 3,598
  • 1
  • 18
  • 33
  • I don't know why you got downvoted but you're right. That was the problem. Thanks, I managed to correct it. :) I had to accept rist's answer because he posted it here first but thanks for the detailed answer. – Isuru Jan 25 '14 at 15:54
  • there is no need to synthesize properties any more – rist Jan 26 '14 at 12:11
  • There is if the property is defined in a protocol. In fact you'll get a warning from Xcode if you don't do so. – Gianluca Tranchedone Jan 26 '14 at 12:28