1

我遇到了一个新问题,但我不知道为什么... Rob 在这里发布了我的进一步解决方案。我喜欢他的作品,并且在 iOS 6.1 更新之前效果很好。

- (void)loadKml:(NSURL *)url
{
    // parse the kml

    Parser *parser = [[Parser alloc] initWithContentsOfURL:url];
    parser.rowElementName = @"Placemark";
    parser.elementNames = @[@"name", @"Snippet", @"coordinates", @"description"];
    parser.attributeNames = @[@"img src="];
    [parser parse];

    // add annotations for each of the entries

    for (NSDictionary *locationDetails in parser.items)
    {
        MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
        annotation.title = locationDetails[@"name"];
        annotation.subtitle = locationDetails[@"Snippet"];
        NSArray *coordinates = [locationDetails[@"coordinates"] componentsSeparatedByString:@","];
        annotation.coordinate = CLLocationCoordinate2DMake([coordinates[1] floatValue], [coordinates[0] floatValue]);
        [self.mapView addAnnotation:annotation];
    }

    // update the map to focus on the region that encompasses all of your annotations

    MKCoordinateRegion region;
    if ([self.mapView.annotations count] > 1)
    {
        region = [self regionForAnnotations:self.mapView.annotations];
        region = MKCoordinateRegionMake(region.center, MKCoordinateSpanMake(region.span.latitudeDelta * 1.05, region.span.longitudeDelta * 1.05));  // expand the region by 5%
    }
    else
    {
        id<MKAnnotation> annotation = self.mapView.annotations[0];
        region = MKCoordinateRegionMakeWithDistance(annotation.coordinate, 100.0, 100.0); // >>>this line throws: "Thread 1: signal SIGABRT"<<<
    }
    [self.mapView setRegion:region animated:YES];
}

自从更新到 iOS 6.1 模拟器后,它就无法工作了。

编辑:我收到此错误:

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
4

2 回答 2

1

几个想法:

  1. 您是否检查过以确保您IBOutletmapView连接已连接?如果self.mapViewnil,应用程序可能会崩溃。

  2. 您是否查看过annotation.coordinate以确保您在那里获得有效的结果?也许有一个MKUserLocation还没有有效值;

  3. 我知道我是给你这个例程的人,但我注意到我们没有检查没有位置的情况。你可能想要这样的东西:

    if ([self.mapView.annotations count] > 0)
    {
        MKCoordinateRegion region;
        if ([self.mapView.annotations count] > 1)
        {
            region = [self regionForAnnotations:self.mapView.annotations];
            region = MKCoordinateRegionMake(region.center, MKCoordinateSpanMake(region.span.latitudeDelta * 1.05, region.span.longitudeDelta * 1.05));  // expand the region by 5%
        }
        else
        {
            id<MKAnnotation> annotation = self.mapView.annotations[0];
            region = MKCoordinateRegionMakeWithDistance(annotation.coordinate, 100.0, 100.0);
        }
        [self.mapView setRegion:region animated:YES];
    }
    
  4. 顺便说一句,我注意到您正在使用:

    parser.attributeNames = @[@"img src="];
    

    那是检索您的图片网址吗?我原以为那应该是:

    parser.attributeNames = @[@"src"];
    

    也许您对解析器进行了一些更改,但attributeDictofdidStartElement永远不会有一个由img src=. 如果 XML 标记是<img src="http://blah.blah.blah/0.jpg">,那么您要查找的属性名称就是src

于 2013-03-19T17:43:17.723 回答
0

您是否检查过您当时实际上有要使用的注释?也许你self.mapView.annotations[0]返回零。

于 2013-03-19T23:02:26.517 回答