I have some questinos regarding iOS6 mapkit. I tried to create a simple application that shows my current location and adds custom annotations to my MapView control. This is my code:
@interface MapViewAnnotation : NSObject<MKAnnotation>{}
@property (nonatomic, copy) NSString *title;
@property (nonatomic, readonly) CLLocationCoordinate2D cordinate;
- (id) initWithTitle:(NSString *) t coordinate:(CLLocationCoordinate2D) c;
@end
And the implementation:
#import "MapViewAnnotation.h"
@implementation MapViewAnnotation
@synthesize title;
@synthesize coordinate;
- (id) initWithTitle:(NSString *) t coordinate:(CLLocationCoordinate2D) c{
self = [super init];
self.title = t;
self.coordinate = c;
return self;
}
@end
And this is UIViewController:
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
@interface ViewController : UIViewController <CLLocationManagerDelegate,MKMapViewDelegate>{}
@property (nonatomic, strong) IBOutlet MKMapView *mapView;
@property (nonatomic, strong) CLLocationManager *locationManager;
@end
And the .main file:
#import "ViewController.h"
#import "MapViewAnnotation.h"
@interface ViewController () @end
@implementation ViewController
@synthesize mapView, locationManager;
- (void)viewDidLoad{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.mapView.delegate = self;
self.locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[self.mapView setShowsUserLocation:YES];
CLLocationCoordinate2D location;
location.latitude = 37.78608;
location.longitude = -122.405398;
MapViewAnnotation *mapAnnotation = [[MapViewAnnotation alloc] initWithTitle:@"Store location" coordinate:location];
[self.mapView addAnnotation:mapAnnotation];
}
-(void) mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views{
MKAnnotationView *annotationView = [views objectAtIndex:0];
id<MKAnnotation> mp = [annotationView annotation];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([mp coordinate], 250, 250);
[self.mapView setRegion:region animated:YES];
}
@end
It displays my current location successfully, but when I use this line in viewDidLoad:
MapViewAnnotation *mapAnnotation = [[MapViewAnnotation alloc] initWithTitle:@"Store location" coordinate:location];
I get this exception:
MapKitDemo[3426:c07] -[MapViewAnnotation setCoordinate:]: unrecognized selector sent to instance 0x8551ae0
2012-10-03 16:19:23.070 MapKitDemo[3426:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MapViewAnnotation setCoordinate:]: unrecognized selector sent to instance 0x8551ae0'
What am I doing wrong?
You are using the property self.coordinate in the
-initWithTitle:coordinate:method of your subclass, this means that the coordinate is being set using the property as opposed to the iVar, since this property is set toreadonly, you are getting this error and probably crash…You must access the iVar directly like so:
Notice I also changed the
@synthesize(which isn’t required anymore – this is done automatically as of Xcode 4.5 I believe? Maybe an earlier version). Also note that I did a condition in your init, you should do this it will make sure you don’t try to et properties/iVars on nil in the case that[super init]failed to initialise your object.