当[mapView addOverlay:overlayPolygon];枚举时,集合发生了变异。

时间:2012-01-21 09:22:14

标签: iphone ios xcode android-mapview

我厌倦了查看SF的解决方案,但无法找到解决方案。 也许我错过了,请帮忙。

我试图通过循环遍历所有KML来检查用户精确定位的多边形。 应用程序总是崩溃@这一点:

            [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];

                }
            }
        } 
    }

4 个答案:

答案 0 :(得分:5)

在遍历数组之前,您可以使用元素创建一个新数组。因此,当原始循环数组发生变异时(由您或其所有者),您循环的数组保持不变。

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

}

因此,如果直接循环SimpleKMLContainer功能,您可以创建一个包含这些功能的临时新数组,并循环遍历该数组。

因为您在addOverlay:上遇到崩溃,所以必须以某种方式循环遍历整个叠加集合。我没有直接在您的代码中看到这一点,因此我假设features集合以某种方式与地图工具包叠加层绑定。

答案 1 :(得分:1)

您可以做的另一件事是不使用for的快速枚举版本。所以尝试替换它:

for (SimpleKMLFeature *feature in ((SimpleKMLContainer *)kml.feature).features)
{
    // Your code here
}

用这个:

for (NSInteger index = 0; index < [((SimpleKMLContainer *)kml.feature).features count]; index++)
{
    SimpleKMLFeature *feature = [((SimpleKMLContainer *)kml.feature).features objectAtIndex:index];
    // Your code here
}

如果能解决您的问题,请告诉我。

答案 2 :(得分:1)

更好地查看名为HazardMap的示例代码,了解如何实施。

答案 3 :(得分:0)

我刚刚通过将我枚举的数组定义为普通NSArray而不是NSMutableArray来解决同样的问题。

这对我有用。