Reputation: 117
I want to display annotations using lat and lon, the lat and lon are coming from JSON(nearbyStop), and i stored the longitude and latitude into two NSArrays. When i try to add annotation to the mapview in the for loop, the EXC_BAD_ACCESS error occurs
Any helps?
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.
}
Here is my 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
and 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
Upvotes: 2
Views: 287
Reputation: 17381
It might not be the cause but this...
@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..
can be reduced to
@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...
as theres no need to declare ivars for properties.
and these
@synthesize phone;
@synthesize time;
@synthesize coordinateL;
can be deleted as you dont need to synthesise properties unless they come from a protocol which title
and subtitle
do in MKAnnotation
which MKPlacemark
conforms to.
Im a little confused as to why you declare coordinateL
when MKPlacemark
already defines coordinate
the other probable problem is that you are not calling the superclass in your init
methods.
so you probably need this in both your init methods...
-(id)initWithCoordinate:(CLLocationCoordinate2D)coord title:(NSString *)title
{
self = [super initWithCoordinate:coord addressDictionary:nil];
if(self) {
self.coordinateL = coord;
self.title = title;
}
return self;
}
Upvotes: 1
Reputation: 549
I would suspect that these lines are the problem:
NSString *longi = stop[@"result"][@"lon"];
and
NSString *longi = stop[@"result"][@"lat"];
because the variable stop
is nil, OR the dictionary stop[@"result"]
is nil. Trying to get a value out of a nil dictionary is going to crash your app, but adding in a nil check for the dictionary (stop
or stop[@"result"]
in this case) should keep it from crashing, but will also give you the opportunity to do some error handling for improper dictionary state.
Upvotes: 0