MKMapView没有正确更新叠加,延迟显示MKPolyline

时间:2012-03-17 10:20:49

标签: cocoa mkmapview

问题在于,当我向MapView添加折线时...折线显示为随机延迟。有时它需要1秒有时5秒

这是绘制折线的功能。

- (void) setRoutePoints:(NSArray*)locations {
    CLLocationCoordinate2D *pointsCoOrds = (CLLocationCoordinate2D*)malloc(sizeof(CLLocationCoordinate2D) * [locations count]);
    NSUInteger i, count = [locations count];
    for (i = 0; i < count; i++) {
        CLLocation* obj = [locations objectAtIndex:i];
        pointsCoOrds[i] = CLLocationCoordinate2DMake(obj.coordinate.latitude, obj.coordinate.longitude);
    }

    [mapView addOverlay:[MKPolyline polylineWithCoordinates:pointsCoOrds count:locations.count]];
    free(pointsCoOrds);
}

所需的回调功能(参见Apple Docs)也是正确的

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id)overlay {
    if ([overlay isKindOfClass:[MKPolyline class]]) {
        MKPolylineView* routeLineView = [[MKPolylineView alloc] initWithPolyline:overlay];
        routeLineView.fillColor = [UIColor colorWithRed:0.0f green:0.0f blue:1.0f alpha:0.5f];
        routeLineView.strokeColor = [UIColor colorWithRed:0.0f green:0.0f blue:1.0f alpha:0.5f];
        routeLineView.lineWidth = 8;
        return routeLineView;
    }
    return nil;
}

这就是我调用函数添加Polyline的方法

[self setRoutePoints:steps];

唯一的问题是在地图上绘制折线的延迟是随机的。

1 个答案:

答案 0 :(得分:0)

解决我的问题的解决方案是调用

setTheRoutePoints

主线程上的函数。

这消除了显示折线的延迟。

[self performSelectorOnMainThread:@selector(setRoutePoints:) withObject:steps waitUntilDone:NO];