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 to readonly
, you are getting this error and probably crash...
You must access the iVar directly like so:
#import "MapViewAnnotation.h"
@implementation MapViewAnnotation
@synthesize title = _title, coordinate = _coordinate
- (id) initWithTitle:(NSString *) t coordinate:(CLLocationCoordinate2D) c{
self = [super init];
if(self){
_title = t;
_coordinate = c;
// self.coordinate = c /* !This is readonly ! */
}
return self;
}
@end
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.
coordinate is a readonly property. You can't use the setter for it. Use the ivar or make it read/write.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.