【发布时间】:2014-03-25 13:02:55
【问题描述】:
我正在尝试使用此代码将带有图标的 MKAnnotation 添加到地图中:
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface MapItem : NSObject<MKAnnotation>
{
CLLocationCoordinate2D coordinate;
}
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, assign) NSString *itemtitle;
@property (nonatomic, assign) NSString *icon;
@property (nonatomic, assign) NSString *itemsubtitle;
- (NSString *)subtitle;
- (NSString *)title;
@end
#import "MapItem.h"
@implementation MapItem
@synthesize coordinate;
@synthesize icon;
@synthesize itemtitle;
@synthesize itemsubtitle;
-(NSString*) title{
return self.itemtitle;
}
-(NSString*) subtitle{
return self.itemsubtitle;
}
@end
这个来自 uiview:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
static NSString *identifier = @"MapItem";
if ([annotation isKindOfClass:[MapItem class]]) {
MKAnnotationView *annotationView = (MKAnnotationView *) [_mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
annotationView.image = [UIImage imageNamed:((MapItem *)annotation).icon];
} else {
annotationView.annotation = annotation;
}
return annotationView;
}
return nil;
}
此代码正在运行,我可以在地图上看到注释,只有当我触摸地图时应用程序崩溃:
2014-03-25 14:53:30.540 PASIL[444:60b] -[__NSArrayM length]: unrecognized selector sent to instance 0x19f26230
2014-03-25 14:53:30.541 PASIL[444:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM length]: unrecognized selector sent to instance 0x19f26230'
*** First throw call stack:
(0x2dc59fd3 0x384d0ccf 0x2dc5d967 0x2dc5c253 0x2dbab7f8 0x2ee10e1f 0x2ee2a5c5 0x2ee2a943 0x304b081f 0x304b069b 0x304a51b3 0x304a4f9f 0x30478e2d 0x2dc2525b 0x2dc2472b 0x2dc22f1f 0x2db8df4f 0x2db8dd33 0x32ae6663 0x304d916d 0xea145 0x389ddab7)
libc++abi.dylib: terminating with uncaught exception of type NSException
添加注释:
for (NSDictionary *dataDict in responseObject) {
if([dataDict objectForKey:@"longitude"]!= (id)[NSNull null])
{
NSString *title=[dataDict objectForKey:@"title"];
NSInteger report_type_id=[[dataDict objectForKey:@"report_type_id"] integerValue];
float longitude=[[dataDict objectForKey:@"longitude"] floatValue];
float latitude=[[dataDict objectForKey:@"latitude"] floatValue];
CLLocationCoordinate2D coordinate;
coordinate.latitude = latitude;
coordinate.longitude = longitude;
MapItem *annotation = [[MapItem alloc] init];
annotation.coordinate=coordinate;
annotation.itemtitle=title;
switch(report_type_id)
{
case 1:
annotation.itemsubtitle=@"Terror";
annotation.icon=@"map-legend-terrorism.png";
break;
case 2:
annotation.itemsubtitle=@"Traffic";
annotation.icon=@"map-legend-traffic.png";
break;
case 3:
annotation.itemsubtitle=@"Weather";
annotation.icon=@"map-legend-weather.png";
break;
case 4:
annotation.itemsubtitle=@"Crime";
annotation.icon=@"map-legend-crime.png";
break;
}
[self.mapView addAnnotation:annotation];
}
}
【问题讨论】:
-
显示如何分配
itemtitle和itemsubtitle。看起来一个或两个都被分配了 NSMutableArray 而不是 NSString。 -
这里是代码(注释出现在地图上——点击它时会崩溃,无论是否在注释上,在尝试缩放/移动时也会崩溃)
-
在
NSString *title = ...行之后,放置这个NSLog,看看它写了什么:NSLog(@"title class = %@", [title class]);。 -
这是输出:title class= __NSCFString 但我无法将其转换为 NSString。评论标题分配正在解决问题。我会寻找一种投射方式。谢谢!
-
所有注释都是那个类吗?
标签: ios objective-c mapkit mkannotation