2013-03-19 77 views
1

我有一個新的問題,我不知道爲什麼...我的進一步的解決方案是由羅布張貼here。我喜歡他的工作,它的工作非常好,直到更新到iOS 6.1。MKCoordinateRegionMakeWithDistance引發「線程1:信號SIGABRT」

- (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模擬器升級以來,它不工作。

編輯:我得到這個錯誤:

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

回答

1

一對夫婦的想法:

  1. 你檢查,以確保您的IBOutletmapView掛了?如果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"]; 
    

    也許你已經取得了一些變化解析器,但didStartElementattributeDict將永遠不會被img src=鍵控的對象。如果XML標記爲<img src="http://blah.blah.blah/0.jpg">,那麼您要查找的屬性名稱僅爲src

+0

1)是鉤2.)我不知道我怎麼能測試它3.)我嘗試了代碼,我的應用程序將運行沒有錯誤,但與空地圖4.)更正:-) – CTSchmidt 2013-03-20 10:33:47

+0

@CurtisTimoSchmidt 2.將NSLog語句放在提供例外的行之前。或者更好的是,通過該代碼放置一個斷點並單步執行。 (請參閱[WWDC 2012調試視頻](https://developer.apple.com/videos/wwdc/2012/?id=415)或參閱[Xcode用戶指南](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)。)當你得到這樣的異常時,它總是因爲一些無效的變量正在傳遞給某些方法/功能。 – Rob 2013-03-20 12:45:17

+0

我已經標註了註釋的代碼行,請參閱:'region = MKCoordinateRegionMakeWithDistance(annotation.coordinate,100.0,100.0);'如果我這樣做'NSLog(@「%@」,annotation.coordinate);'befor and after這行我會得到一個警告:'格式指定類型「ID」,但參數的類型CCLocationCoordinate2D'我有點困惑 – CTSchmidt 2013-03-20 13:28:13

0

你有沒有檢查過你實際上有一個註釋要在那一點上使用?也許你的self.mapView.annotations[0]返回零。