2012-03-17 65 views
0

問題是,當我向MapView中添加折線時...折線顯示爲隨機延遲。 Somtimes花了1秒,有時5秒MKMapView不能正確更新覆蓋圖,延遲顯示MKPolyline

這是繪製折線的函數。

- (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); 
} 

需要(見蘋果文檔)回調函數也是正確的

- (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; 
} 

這是我如何打電話添加折線

[self setRoutePoints:steps]; 

唯一的問題是,該函數折線在地圖上繪製的延遲是隨機的。

回答

0

是解決我的問題的解決方法是調用主線程上

setTheRoutePoints 

功能。

這消除了顯示折線的延遲。

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