[ios]集合突变时被枚举时 [地图查看 addOverlay:overlayPolygon];

标签: Xcode iphone ios
发布时间: 2017/3/27 19:37:33
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

在 SF 寻找解决方案,累了,但找不到解决方案。 也许我错过了它请帮助。

我试图查阅用户精确定位与多边形的遍历所有的 KMLs。 @ 这点总是崩溃的应用程序︰

            [mapView addOverlay:overlayPolygon];

            // zoom the map to the polygon bounds
            [mapView setVisibleMapRect:overlayPolygon.boundingMapRect animated:YES];

问题代码︰

//create KML in hidden Mapview
-(void)loadKML:(NSMutableArray *)kmlNameArray
{    
    //dispatch_group_t group = dispatch_group_create();


    //remove polygon and redraw again.
    [NSThread detachNewThreadSelector: @selector(spinEnd) toTarget:self withObject:nil];

    [mapView removeOverlays:mapView.overlays];
    [inUserRangeArray removeAllObjects];
    [inUserRangeArrayObjectIndex removeAllObjects];
    [scrollview removeFromSuperview];
    [pageControl removeFromSuperview];
    [NSThread detachNewThreadSelector: @selector(spinBegin) toTarget:self withObject:nil];

    NSArray *sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
    NSString *docDirectory = [sysPaths objectAtIndex:0];   


    for (int e=0; e<[kmlNameArray count]; e++) 
    {        
        //NSString *kmlNameStr = [kmlNameArray objectAtIndex:e];
        Frog *kmlID = [self.fs objectAtIndex:e];
        self.kmlID = [NSString stringWithFormat:@"%i",kmlID.fID];
        self.kmlIDObjectIndex = [NSString stringWithFormat:@"%i",e];

        NSLog(@"asasas %@",kmlIDObjectIndex);

        //NSLog(@"KML items %@", kmlNameStr);          
        //NSLog(@"KML ID %@", kmlID);
        //NSLog(@"KML file Path %@",[NSString stringWithFormat:@"%@/data/%@/%@", docDirectory,self.kmlID,[kmlNameArray objectAtIndex:e]]);


        SimpleKML *kml = [SimpleKML KMLWithContentsOfFile:[NSString stringWithFormat:@"%@/data/%@/%@", docDirectory,self.kmlID,[kmlNameArray objectAtIndex:e]]error:NULL];

        // look for a document feature in it per the KML spec


        //        dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{

        if (kml.feature && [kml.feature isKindOfClass:[SimpleKMLDocument class]])
        {// see if the document has features of its own
            for (SimpleKMLFeature *feature in ((SimpleKMLContainer *)kml.feature).features)
            {// otherwise, see if we have any placemark features with a polygon
                if ([feature isKindOfClass:[SimpleKMLPlacemark class]] && ((SimpleKMLPlacemark *)feature).polygon)
                {
                    SimpleKMLPolygon *polygon = (SimpleKMLPolygon *)((SimpleKMLPlacemark *)feature).polygon;
                    SimpleKMLLinearRing *outerRing = polygon.outerBoundary;
                    //points[i], i = number of coordinates
                    CLLocationCoordinate2D points[[outerRing.coordinates count]];
                    NSUInteger i = 0;
                    for (CLLocation *coordinate in outerRing.coordinates)
                    { 
                        points[i++] = coordinate.coordinate;
                    }
                    // create a polygon annotation for it
                    self.overlayPolygon = [MKPolygon polygonWithCoordinates:points count:[outerRing.coordinates count]];

                    //crash here
                    [mapView addOverlay:overlayPolygon];

                    // zoom the map to the polygon bounds
                    [mapView setVisibleMapRect:overlayPolygon.boundingMapRect animated:YES];

                }
            }
        } 
    }

解决方法 1:

您遍历数组之前,你可以创建一个新的数组的元素。所以当原环阵列突变 (由你,或由其所有者) 您遍历数组也保持不变。

NSArray *theFeatures = [NSArray arrayWithObjects:((SimpleKMLContainer *)kml.feature).features];
for (SimpleKMLFeature *feature in theFeatures) {

}

所以在循环结束的情况下 SimpleKMLContainer 直接功能,您使用这些功能,创建一个临时的新数组和遍历该数组。

因为你经验的崩溃 addOverlay: 你必须以某种方式在整个覆盖集合的循环。我看不到,直接在您的代码,所以我假定 features 集合不知何故绑在地图套件覆盖。

赞助商