Issue
i tired to look over SF for solution, but can't find a solution. maybe i missed it please help.
i tried to check out a user pinpoint with polygon by looping through all the KMLs. the app always crash @ this point:
[mapView addOverlay:overlayPolygon];
// zoom the map to the polygon bounds
[mapView setVisibleMapRect:overlayPolygon.boundingMapRect animated:YES];
problem code:
//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];
}
}
}
}
Solution
Before you loop over the array, you can create a new array with the elements. So when the original looped array is mutated (either by you, or by its owner) the array you loop over stays intact.
NSArray *theFeatures = [NSArray arrayWithObjects:((SimpleKMLContainer *)kml.feature).features];
for (SimpleKMLFeature *feature in theFeatures) {
}
So in case of looping over the SimpleKMLContainer
features directly, you create a temporary new array with those features, and loop over that array.
Because you experience a crash on addOverlay:
you must somehow be looping over the entire overlay collection. I don't see that directly in your code, so I assume that the features
collection is somehow tied to the map kit overlays.
Answered By - Joris Kluivers
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.