Search code examples
iosmkmapviewmkannotationxcode4.2mkpinannotationview

Not able to initialize CLLocationCoordinate2D iVar in MKAnnotation Classe


I am trying to put MKAnnotations on MKMapView. I am following the procedure that has been suggested on various available tutorials. I have created PlaceMark.h/.m file from NSobject Class as following. PlaceMark.h

#import <Foundation/Foundation.h>
#import <MapKit/MKAnnotation.h>
@interface PlaceMark : NSObject<MKAnnotation>
{
CLLocationCoordinate2D coordinate;
NSString *title;
}
@property (nonatomic,copy) NSString *title;
@property (nonatomic,readonly) CLLocationCoordinate2D coordinate;
@end

PlaceMark.m

#import "PlaceMark.h"

@implementation PlaceMark
@synthesize coordinate,title;

-(void)dealloc
{
   [title release];
   [super dealloc];
}

@end

in my viewController which holds MKMapView, in viewDidload I have following code

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

MKCoordinateRegion region;
region.center.latitude=37.3305262;
region.center.longitude=-122.0290935;
region.span.latitudeDelta=0.01;
region.span.longitudeDelta=0.01;

[parkingMap setRegion:region animated:YES];

PlaceMark *ann=[[[PlaceMark alloc]init]autorelease]; // I have also tired it using explicitly defining method -(id)initwithTitle.... but it did not worked.

ann.title=@"Test";
ann.coordinate= region.center;
[parkingMap addAnnotation:ann];
}

Can anyone please tell me what I am doing wrong? BTW I am using Xcode4.2 with iOS sdk5.0 and Not using storyboards/automatic reference counting

Thanks Sumit


Solution

  • You've defined the coordinate property as readonly so you can't set it.

    Change it to readwrite.

    Also, if you just need a basic annotation object with a title, subtitle, and settable coordinate, you can use the built-in MKPointAnnotation class instead of defining your own class. The creation and initialization would be identical (just replace your class name with MKPointAnnotation).