gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 19:01:28 25 4
gpt4 key购买 nike

我正在尝试使用以下代码将带有图标的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com