1

i got a new issue and i don't know why... My further solution was here posted by Rob. I love his work and it works very well until the update comes to 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];
}

It is not working since the update to iOS 6.1 Simulator.

EDIT: i get this error:

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
Community
  • 1
  • 1
CTSchmidt
  • 1,155
  • 3
  • 17
  • 30

2 Answers2

1

A couple of thoughts:

  1. Have you checked to make sure your IBOutlet to your mapView is hooked up? If self.mapView was nil, the app might crash.

  2. Have you looked at the annotation.coordinate to make sure you're getting a valid result there? Perhaps there is a MKUserLocation that doesn't have valid values yet;

  3. I know I'm the one who gave you that routine, but I notice that we're not checking for the situation where there were no locations. You probably want something like:

    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. As an aside, I notice that you're using:

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

    is that retrieving your image URL? I would have thought that that should just be:

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

    Maybe you've made some change to the parser, but the attributeDict of didStartElement will never have an object keyed by img src=. If the XML tag was <img src="http://blah.blah.blah/0.jpg">, the attribute name you're looking for is just src.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • 1.) Yes hooked 2.) i don't know how i can test it 3.) I tryed the code and my App will run without errors but with an empty map 4.) corrected :-) – CTSchmidt Mar 20 '13 at 10:33
  • @CurtisTimoSchmidt 2. Put a NSLog statement right before the line that's giving you the exception. Or better, put a breakpoint in and single step through that code. (See [WWDC 2012 Debug Video](https://developer.apple.com/videos/wwdc/2012/?id=415) Or see [Xcode users guide](https://developer.apple.com/library/ios/documentation/ToolsLanguages/Conceptual/Xcode_User_Guide/060-Debug_and_Tune_Your_App/debug_app.html#//apple_ref/doc/uid/TP40010215-CH3-SW38).) When you get an exception like this, it`s invariably because some invalid variable is being passed to some method/function. – Rob Mar 20 '13 at 12:45
  • The line of code I have marked with a comment, see: `region = MKCoordinateRegionMakeWithDistance(annotation.coordinate, 100.0, 100.0);` if i do `NSLog(@"%@", annotation.coordinate);` befor and after this line I will get a warning: `Format specifies type "id" but the argument has type CCLocationCoordinate2D` I am a bit confused – CTSchmidt Mar 20 '13 at 13:28
  • @CurtisTimoSchmidt Yeah, so you could do `NSLog(@"%@: %f, %f", annotation, annotation.coordinate.latitude, annotation.coordinate.longitude);` – Rob Mar 20 '13 at 15:03
  • Unfortunately, it has brought nothing. There are no details in the log displayed. The app always terminate after `id annotation = self.mapView.annotations[0];` – CTSchmidt Mar 20 '13 at 15:57
  • @CurtisTimoSchmidt That means it's not getting to that line of code. In your original question you said that you were getting "Thread 1: signal SIGABRT". But I now notice that you updated the question with a different error, "index 0 beyond bounds". Sounds like your map view has no annotations, and thus the attempt to get the first one is failing. See #3 in my answer (where I suggested you check to see if you have some annotations before you try to set the region based upon the annotations), which should address that. – Rob Mar 20 '13 at 16:04
  • I'm very sorry ... the server was offline so the error caused. The file could not be loaded. Everything works perfectly. – CTSchmidt Mar 21 '13 at 09:40
0

have you checked you actually have an annotation to use at that point? Maybe your self.mapView.annotations[0] returned nil.

Craig
  • 8,093
  • 8
  • 42
  • 74