【问题标题】:EXC_BAD_ACCESS error occurs when add annotation to MKMapView向 MKMapView 添加注释时发生 EXC_BAD_ACCESS 错误
【发布时间】:2016-04-03 21:32:49
【问题描述】:

我想使用 lat 和 lon 显示注释,lat 和 lon 来自 JSON(nearbyStop),我将经度和纬度存储到两个 NSArrays 中。当我尝试在 for 循环中向 mapview 添加注释时,会发生 EXC_BAD_ACCESS 错误 有什么帮助吗?

    NSMutableArray *coordinateLongi = [[NSMutableArray alloc]init];
for (NSDictionary *stop in nearbyStop) {
    NSString *longi = stop[@"result"][@"lon"];
    if(longi == nil)
    {
        NSLog(@"there is no data");
    }
    else
    {
        [coordinateLongi addObject:longi];
    }
}

NSMutableArray *coordinateLatit = [[NSMutableArray alloc]init];
for (NSDictionary *stop in nearbyStop) {
    NSString *latit = stop[@"result"][@"lat"];
    if(latit == nil)
    {
        NSLog(@"there is no data");
    }
    else
    {
        [coordinateLatit addObject:latit];
    }
}

for(int i = 0; i<coordinateLongi.count;i++)
{
    CLLocationCoordinate2D coord;
    coord.latitude = [[NSString stringWithFormat:@"%@",[coordinateLongi objectAtIndex:i]] floatValue];
    for(int j = 0; j<coordinateLatit.count;j++)
    {
        coord.longitude = [[NSString stringWithFormat:@"%@",[coordinateLatit objectAtIndex:i]] floatValue];
    }


    CustomAnnotation *annotObj = [[CustomAnnotation alloc] initWithCoordinate:coord title:@"title" ];
    [map addAnnotation:annotObj];//Error occurs here.
}

这是我的 customAnnotation.h

 #import <MapKit/MapKit.h>

@interface CustomAnnotation : MKPlacemark
{
    CLLocationCoordinate2D coordinateL;
    NSString *title;
    NSString *subtitle;
    NSString *time;
}

@property (nonatomic)CLLocationCoordinate2D coordinateL;
@property (strong, nonatomic) NSString *title;
@property (strong, nonatomic) NSString *subtitle;
@property (strong, nonatomic) NSString *phone;
@property (strong, nonatomic) NSString *time;

-(id)initWithCoordinate:(CLLocationCoordinate2D) c  title:(NSString *) t  subTitle:(NSString *)timed time:(NSString *)tim;

-(id)initWithCoordinate:(CLLocationCoordinate2D) c title:(NSString *)tit;



@end

和 customAnnotation.m

    #import "CustomAnnotation.h"

@implementation CustomAnnotation

@synthesize title;
@synthesize subtitle;
@synthesize phone;
@synthesize time;
@synthesize coordinateL;


-(id)initWithCoordinate:(CLLocationCoordinate2D) c  title:(NSString *) t  subTitle:(NSString *)timed time:(NSString *)tim
{
    self.coordinateL=c;
    self.time=tim;
    self.subtitle=timed;
    self.title=t;
    return self;
}

-(id)initWithCoordinate:(CLLocationCoordinate2D) c title:(NSString *)tit
{
    self.coordinateL=c;
    self.title=tit;
    return self;
}

@end

【问题讨论】:

    标签: ios objective-c mkmapview mkannotation


    【解决方案1】:

    这可能不是原因,但这...

    @interface CustomAnnotation : MKPlacemark
    {
        CLLocationCoordinate2D coordinateL;
        NSString *title;
        NSString *subtitle;
        NSString *time;
    }
    
    @property (nonatomic)CLLocationCoordinate2D coordinateL;
    @property (strong, nonatomic) NSString *title;
    @property (strong, nonatomic) NSString *subtitle;
    @property (strong, nonatomic) NSString *phone;
    @property (strong, nonatomic) NSString *time;
    
    etc..
    

    可以简化为

    @interface CustomAnnotation : MKPlacemark
    
    @property (nonatomic)CLLocationCoordinate2D coordinateL;
    @property (strong, nonatomic) NSString *title;
    @property (strong, nonatomic) NSString *subtitle;
    @property (strong, nonatomic) NSString *phone;
    @property (strong, nonatomic) NSString *time;
    
    etc...
    

    因为不需要为属性声明 ivars。

    还有这些

    @synthesize phone;
    @synthesize time;
    @synthesize coordinateL;
    

    可以删除,因为您不需要综合属性,除非它们来自titlesubtitleMKAnnotation 中执行的MKPlacemark 符合的协议。

    MKPlacemark 已经定义coordinate 时,我有点困惑为什么你声明coordinateL

    另一个可能的问题是您没有在 init 方法中调用超类。

    所以你可能在你的两个初始化方法中都需要这个......

    -(id)initWithCoordinate:(CLLocationCoordinate2D)coord title:(NSString *)title
    {
        self = [super initWithCoordinate:coord addressDictionary:nil];
        if(self) {
          self.coordinateL = coord;
          self.title = title;
        }
        return self;
    }
    

    【讨论】:

    • 感谢沃伦的帮助!我已经解决了这个问题。我正在做的是尝试在地图上显示不同类型的注释,这就是为什么它让我感到困惑。一种类型是附近的餐馆,我使用 MKLocalSearchRequest 搜索它们并添加注释。另一种类型是附近停靠点,我使用 NSObject 超类创建另一个注释并声明坐标。现在我弄清楚 NSObject 和 MKPlacemark 超类之间的区别:)
    【解决方案2】:

    我怀疑这些行是问题所在:

    NSString *longi = stop[@"result"][@"lon"];

    NSString *longi = stop[@"result"][@"lat"];

    因为变量stop 为零,或者字典stop[@"result"] 为零。尝试从 nil 字典中获取值会使您的应用程序崩溃,但添加对字典的 nil 检查(在这种情况下为stopstop[@"result"])应该可以防止它崩溃,但也会给你有机会对不正确的字典状态进行一些错误处理。

    【讨论】:

    • nah.. 我使用 NSLog 来检查坐标Latit 和坐标Longi NSArray。经度和纬度信息已经存在。但是addAnnovation的时候,出现错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多