使用GCD解析KML与Apple的KMLViewer

时间:2013-08-21 03:26:06

标签: kml grand-central-dispatch

我正在使用Apple的KMLViewer加载KML文件并将其显示在MapView中。 KML文件中有超过50,000行坐标,这当然会导致它加载缓慢。为了加快速度,我正在尝试使用GCD在另一个线程中执行解析。

只要显示得当且速度可以接受,我就能很好地工作。但是,我在加载地图时遇到间歇性的运行时错误。我怀疑这是因为我的布局方式,UI正在GCD块内更新。我正在阅读的所有内容都说UI应该在主线程中更新,否则可能会发生运行时错误,这些错误是间歇性的,很难追踪。嗯,这就是我所看到的。

问题是,我无法弄清楚如何在主线程中更新UI。我还是iOS编程的新手,所以我只是把东西扔在墙上看看有什么用。这是我的代码,基本上是Apple的KMLViewerViewController.m,有一些修改:

    #import "KMLViewerViewController.h"

@implementation KMLViewerViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    activityIndicator.hidden = TRUE;


    dispatch_queue_t myQueue = dispatch_queue_create("My Queue",NULL);
    dispatch_async(myQueue, ^{

    // Locate the path to the route.kml file in the application's bundle
    // and parse it with the KMLParser.

        NSString *path = [[NSBundle mainBundle] pathForResource:@"BigMap" ofType:@"kml"];
        NSURL *url = [NSURL fileURLWithPath:path];
        kmlParser = [[KMLParser alloc] initWithURL:url];
        [kmlParser parseKML];

        dispatch_async(dispatch_get_main_queue(), ^{

    // Update the UI


    // Add all of the MKOverlay objects parsed from the KML file to the map.
    NSArray *overlays = [kmlParser overlays];
    [map addOverlays:overlays];

    // Add all of the MKAnnotation objects parsed from the KML file to the map.
    NSArray *annotations = [kmlParser points];
    [map addAnnotations:annotations];



    // Walk the list of overlays and annotations and create a MKMapRect that
    // bounds all of them and store it into flyTo.
    MKMapRect flyTo = MKMapRectNull;
    for (id <MKOverlay> overlay in overlays) {
        if (MKMapRectIsNull(flyTo)) {
            flyTo = [overlay boundingMapRect];
        } else {
            flyTo = MKMapRectUnion(flyTo, [overlay boundingMapRect]);
        }
    }

    for (id <MKAnnotation> annotation in annotations) {
        MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
        MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0);
        if (MKMapRectIsNull(flyTo)) {
            flyTo = pointRect;
        } else {
            flyTo = MKMapRectUnion(flyTo, pointRect);
        }
    }

    // Position the map so that all overlays and annotations are visible on screen.
    map.visibleMapRect = flyTo;

        });
    });

}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    activityIndicator.hidden = FALSE;
    [activityIndicator startAnimating];
}


#pragma mark MKMapViewDelegate

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
    return [kmlParser viewForOverlay:overlay];
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    return [kmlParser viewForAnnotation:annotation];
}

- (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView
{
    [activityIndicator stopAnimating];
    activityIndicator.hidden = TRUE;
}


@end

建议?

0 个答案:

没有答案