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 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
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 :) –