ios - MKMapView 与带有自定义图标的 MKAnnotation 一起崩溃

标签 ios objective-c mapkit mkannotation

我正在尝试使用以下代码将带有图标的 MKAnnotation 添加到 map 中作为注释:

#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;
}

此代码正在运行,我可以在 map 上看到注释,只有当我触摸 map 时应用程序崩溃:
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];
            }



        }

最佳答案

问题在于 NSString MapItem 中的属性类被声明(不是标题之一是我评论中建议的数组):

@property (nonatomic, assign) NSString *itemtitle;
@property (nonatomic, assign) NSString *icon;
@property (nonatomic, assign) NSString *itemsubtitle;

通过将属性声明为 assign ,注释对象不会保留这些值,并且稍后的内存(本地图 View 尝试访问它时)指向其他东西(在您的情况下,它恰好是看起来像一个数组的东西)。

将声明更改为此(将 assign 更改为 copy ):
@property (nonatomic, copy) NSString *itemtitle;
@property (nonatomic, copy) NSString *icon;
@property (nonatomic, copy) NSString *itemsubtitle;

您稍后将遇到的一个单独的、不相关的问题是注释 View 的重用:
现在,在 viewForAnnotation , image仅在为某些注释“x”创建 View 时(当 annotationView == nil 时)设置 View 。

但是,如果 View 被重新用于另一个注释“y”,image仍将设置为最初为注释“x”创建 View 时的值。

要解决此潜在问题,请移动 image在 if-else block 之后分配:
    //Don't assign the image here
    //(because it's specific to the current annotation)
    //annotationView.image = [UIImage imageNamed:((MapItem *)annotation).icon];
} else {
    annotationView.annotation = annotation;
}

//Assign the image here...
//(because it's specific to the current annotation)
annotationView.image = [UIImage imageNamed:((MapItem *)annotation).icon];

return annotationView;

关于ios - MKMapView 与带有自定义图标的 MKAnnotation 一起崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22635498/

相关文章:

ios - prepareForSegue 函数不起作用

iphone - 使用 Core Data 实现枚举的最佳方法

ios - Interface Builder 运行时自动布局约束阻止 View 占用固有大小,如何解决此问题?

ios - 在运行时动态创建文件到 Watch Extension

ios - 无法按住 Ctrl 键将 View 拖到头文件

ios - 使用数组在 UITableView 中具有不同标签颜色的不同单元格

ios - 下拉列表中的 map 位置名称

ios - MapKit:路线未显示在两个注释之间

ios - 链接器命令无法在 ios 中执行代码 1(使用 -v 查看调用)

objective-c - 无法在 AVPlayer 中播放 mp4 视频