1

新しい問題が発生しましたが、理由がわかりません...私のさらなる解決策は、Robによって投稿されました私は彼の仕事が大好きで、アップデートが 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];
}

iOS 6.1 Simulator にアップデートしてから動作しません。

編集:私はこのエラーが発生します:

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

2 に答える 2

1

いくつかの考え:

  1. IBOutletが接続されていることを確認しmapViewましたか? self.mapViewだった場合nil、アプリがクラッシュする可能性があります。

  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="];
    

    画像の URL を取得していますか? 私はそれがちょうどあるべきだと思っていたでしょう:

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

    パーサーに何らかの変更を加えたかもしれませんが、attributeDictの はdidStartElementをキーとするオブジェクトを持つことはありませんimg src=。XML タグが だった場合、<img src="http://blah.blah.blah/0.jpg">探している属性名はsrc.

于 2013-03-19T17:43:17.723 に答える
0

その時点で実際に使用する注釈があることを確認しましたか? 多分あなたのself.mapView.annotations[0]返されたnil。

于 2013-03-19T23:02:26.517 に答える