2016-04-03 10 views
1

Ich möchte Annotationen mit lat und lon anzeigen, lat und lon kommen von JSON (in der Nähe von Stop), und ich speicherte den Längen- und Breitengrad in zwei NSArrays. Wenn ich versuche, der MapView in der for-Schleife eine Annotation hinzuzufügen, tritt der EXC_BAD_ACCESS-Fehler auf enter image description here Irgendwelche Hilfen?EXC_BAD_ACCESS Fehler beim Hinzufügen von Annotation zu MKMapView

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. 
} 

Hier ist meine 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 

und 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 

Antwort

1

Es ist vielleicht nicht die Ursache sein, aber dies ...

@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.. 

kann

@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... 

reduziert werden, da keine Notwendigkeit, Theres ivars für Eigenschaften zu erklären.

und diese

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

können, wie Sie Eigenschaften müssen zur Synthetisierung nicht gelöscht werden, wenn sie aus einem Protokoll kommen die title und subtitle in MKAnnotation tun, die MKPlacemark zu entspricht.

Im ein wenig verwirrt, warum Sie coordinateL erklären, wenn MKPlacemark bereits definiert coordinate

das andere wahrscheinliche Problem ist, dass Sie nicht die übergeordnete Klasse in Ihrem init Methoden aufrufen.

so müssen Sie es wahrscheinlich sowohl in Ihrer init Methoden ...

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

Vielen Dank für Ihre Hilfe Warren! Ich habe das Problem bereits gelöst. Ich habe versucht, verschiedene Arten von Anmerkungen auf der Karte anzuzeigen, deshalb verwirrte es mich. Eine Art ist die nahe gelegenen Restaurants, ich habe MKLocalSearchRequest verwendet, um sie zu suchen und Anmerkungen hinzuzufügen. Ein anderer Typ ist ein Stopp in der Nähe. Ich erstelle eine weitere Annotation mit der Superklasse NSObject und deklariere die Koordinate.Jetzt finde ich den Unterschied zwischen NSObject und MKPlacemark Superklasse :) –

0

Ich würde vermuten, dass diese Linien sind das Problem:

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

und

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

, da die Variable stop ist gleich Null, oder das Wörterbuch stop[@"result"] ist gleich Null. Der Versuch, einen Wert aus einem Null-Wörterbuch zu erhalten, wird Ihre App zum Absturz bringen, aber das Hinzufügen eines Null-Checks für das Wörterbuch (stop oder stop[@"result"] in diesem Fall) sollte verhindern, dass es abstürzt, aber Ihnen auch die Möglichkeit geben, etwas zu tun Fehlerbehandlung für einen falschen Wörterbuchzustand.

+0

nah .. i NSLog verwendet coordinateLatit und coordinateLongi NSArray zu inspizieren. Die Lon und Lat Informationen sind schon da. Aber wenn addAnnovation, tritt der Fehler –

+0

auf dieses Problem den ganzen Tag ... –