2012-03-17 77 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];