将多组注释加载到地图工具包地图上

时间:2013-11-25 20:53:35

标签: ios json mapkit mkannotation

我试图在地图上同时显示两个json文件。但是有一个问题它给了我奇怪的错误,我无法弄明白。原来那里有

 - (void)viewDidLoad {
    [super viewDidLoad];
    NSMutableArray * annotations = [[NSMutableArray alloc] init];

    self.mapView.visibleMapRect = MKMapRectMake(135888858.533591, 92250098.902419, 190858.927912, 145995.678292);
    NSLog(@"Loading data…");
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
        NSData * JSONData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:self.seedFileName ofType:@"json"]];


        for (NSDictionary * annotationDictionary in [NSJSONSerialization JSONObjectWithData:JSONData options:kNilOptions error:NULL])
        {
            ADClusterableAnnotation * annotation = [[ADClusterableAnnotation alloc] initWithDictionary:annotationDictionary];
            [annotations addObject:annotation];
            [annotation release];
        }

     dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"Building KD-Tree…");
            [self.mapView setAnnotations:annotations];
        });
    });

    [annotations release];
}

所以我看到我有两个ViewControllers来保存seedfile 这个是

#import "CDStreetlightsMapViewController.h"

@implementation CDStreetlightsMapViewController

- (NSString *)pictoName {
    return @"CDStreetlight.png";
}

- (NSString *)clusterPictoName {
    return @"CDStreetlightCluster.png";
}

- (NSString *)seedFileName {
    return @"CDStreetlights";
}
- (NSString *)seedFileName1 {
    return @"CDToilets";
}
@end

另一个是

#import "CDToiletsMapViewController.h"

@implementation CDToiletsMapViewController

- (NSString *)seedFileName {
    return @"CDToilets";
}

- (NSString *)pictoName {
    return @"CDToilet.png";
}

- (NSString *)clusterPictoName {
    return @"CDToiletCluster.png";
}

@end

json文件名为CDToilets和CDStreetlights ...但我有一个标签栏,其中包含厕所和路灯。但是,让我说我想在厕所viewController上显示路灯和厕所?多数民众赞成我的问题是对的..我试过这个

- (void)viewDidLoad {
    [super viewDidLoad];
    NSMutableArray * annotations = [[NSMutableArray alloc] init];

    self.mapView.visibleMapRect = MKMapRectMake(135888858.533591, 92250098.902419, 190858.927912, 145995.678292);
    NSLog(@"Loading data…");

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
        NSData * JSONData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:self.seedFileName ofType:@"json"]];


        for (NSDictionary * annotationDictionary in [NSJSONSerialization JSONObjectWithData:JSONData options:kNilOptions error:NULL])
        {
            ADClusterableAnnotation * annotation = [[ADClusterableAnnotation alloc] initWithDictionary:annotationDictionary];
            [annotations addObject:annotation];
            [annotation release];
        }

        NSData * JSONData1 = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:self.seedFileName1 ofType:@"json"]];


        for (NSDictionary * annotationDictionary in [NSJSONSerialization JSONObjectWithData:JSONData1 options:kNilOptions error:NULL])
        {
            ADClusterableAnnotation * annotation = [[ADClusterableAnnotation alloc] initWithDictionary:annotationDictionary];
            [annotations addObject:annotation];
            [annotation release];
        }

        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"Building KD-Tree…");
            [self.mapView setAnnotations:annotations];
        });
    });

    [annotations release];   
}

我在CDStreetlightsMapViewController seedfile中重命名为seedfile1所以我可以在viewDidLoad中使用它两次它没有显示错误但它运行不正常在模拟器上,我得到一个例外:

ClusterDemo[11014:c07]Loading data…
ClusterDemo[11014:1303]***Assertion failure in -[CDToiletsMapViewController seedFileName1], ADClusterMapView-master-7/ClusterDemo/Classes/CDMapViewController.‌​m:86 2013-11-25 23:59:37.524
ClusterDemo[11014:1303]***Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'This abstract method must be overridden!' *** First throw call stack: (0x18b2012 0x1277e7e 0x18b1e78 0xd0df35 0x3fad 0x36cb 0x2deb53f 0x2dfd014 0x2dee2e8 0x2dee450 0x99843e72 0x9982bdaa) libc++abi.dylib: terminate called throwing an exception (lldb) 

您可以在此处下载完整的应用... https://github.com/applidium/ADClusterMapView

1 个答案:

答案 0 :(得分:0)

如果您阅读了异常,则很明显您正在使用包含断言的抽象方法,以确保您知道自己犯了错误。您需要在seedFileName1中实施CDToiletsMapViewController方法(就像您在CDStreetlightsMapViewController中所做的那样)。

从代码中我猜你需要添加:

- (NSString *)seedFileName1 {
    return @"CDStreetlights";
}