ios - MKCoordinateRegionMakeWithDistance 抛出 "Thread 1: signal SIGABRT"

标签 ios ios6 mkmapview mapkit

我遇到了一个新问题,我不知道为什么...我进一步的解决方案是 Rob 发布的 here。我喜欢他的作品,在更新到 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 Simulator 后,它就不能用了。

编辑:我得到这个错误:

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

最佳答案

几个想法:

  1. 您检查过以确保您的 IBOutlet给你的mapView连接了吗?如果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"];
    

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

关于ios - MKCoordinateRegionMakeWithDistance 抛出 "Thread 1: signal SIGABRT",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15505573/

相关文章:

ios - 无法在 tableView iOS 中获取已解析的数组

objective-c - 如何使用 NSMutableAttributedString 呈现多行 UILabel

iphone - Xamarin IOS - 如何将现有的 sqlite 数据库文件包含到解决方案中

ios - 从 map View 上长按触发推送 segue

xcode - MKMapView setRegion : animated: crashes the app (edited)

ios - GPUImage 直方图和丢帧

ios - Swift:如果 PFObject = nil 则执行 segue。

ios - 我如何摆脱这个警告

objective-c - NSScanner 无法识别 scanUpToString 上的选择器

iphone - 缩放以适合所有注释的区域 - 最终在注释之间放大