注解显示在地图上,但是当我在一个点击,我的git此异常:NSInvalidArgumentException当试图调用注解
[NSNull length]: unrecognized selector sent to instance
'NSInvalidArgumentException', reason: '-[NSNull length]: unrecognized selector sent to instance
我相关的代码是这样的:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
static NSString *identifier = @"MyLocation";
if ([annotation isKindOfClass:[MyLocation class]]) {
MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [mapView2 dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
} else {
annotationView.annotation = annotation;
}
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
annotationView.pinColor = MKPinAnnotationColorRed;
return annotationView;
}
return nil;
}
而且配置注释:
NSNumber * latitude = [[row objectAtIndex:21]objectAtIndex:1];
NSNumber * longitude = [[row objectAtIndex:21]objectAtIndex:2];
NSString * crimeDescription = [row objectAtIndex:17];
NSString * address = [row objectAtIndex:13];
CLLocationCoordinate2D coordinate;
coordinate.latitude = latitude.doubleValue;
coordinate.longitude = longitude.doubleValue;
MyLocation *annotation = [[MyLocation alloc] initWithName:crimeDescription address:address coordinate:coordinate] ;
[mapView2 addAnnotation:annotation];
而且在MyLocation
类中包含注释:
- (id)initWithName:(NSString*)name address:(NSString*)address coordinate:(CLLocationCoordinate2D)coordinate {
if ((self = [super init])) {
_name = [name copy];
_address = [address copy];
_coordinate = coordinate;
}
return self;
}
也许这 http://stackoverflow.com/questions/2377833/mkmapkit-exception-when-using-canshowcallout-on-annotation-view – 2012-03-16 00:57:41