MKOverlayView性能

我在地图上添加了大约3000个MKOverlay,正如你所想象的那样,有时需要一些时间,大约有八秒。 我正在寻找一种使用线程来提高性能的方法,因此用户可以在添加覆盖图时移动地图。 优选地,叠加层将按顺序添加,从地图区域内的开始。 我已经尝试了一些与GCD有关的事情:

- (MKOverlayView*)mapView:(MKMapView*)mapView viewForOverlay:(id)overlay { __block MKPolylineView* polyLineView; //do the heavy lifting (I presume this is the heavy lifting part, but // because this code doesn't compile, I can't actually *test* it) // on a background thread dispatch_async(backgroundQueue, ^ { polyLineView = [[[MKPolylineView alloc] initWithPolyline:overlay] autorelease]; [polyLineView setLineWidth:11.0]; //if the title is "1", I want a blue line, otherwise red if([((LocationAnnotation*)overlay).title intValue]) { [polyLineView setStrokeColor:[UIColor blueColor]]; } else { [polyLineView setStrokeColor:[UIColor redColor]]; } //return the overlay on the main thread dispatch_async(dispatch_get_main_queue(), ^(MKOverlayView* polyLineView){ return polyLineView; }); }); } 

但是因为GCD块是用void参数和返回types定义的,所以这段代码不起作用 – 我在return行中得到一个不兼容的指针types错误。 有什么我在这里失踪,或另一种方式来线程? 或者,也许完全不同的方法来提高叠加过程的性能? 我感谢任何和所有的帮助!

编辑:

我发现问题在于我在这里添加覆盖:

 for(int idx = 1; idx < sizeOverlayLat; idx++) { CLLocationCoordinate2D coords[2]; coords[0].latitude = [[overlayLat objectAtIndex:(idx - 1)] doubleValue]; coords[0].longitude = [[overlayLong objectAtIndex:(idx - 1)] doubleValue]; coords[1].latitude = [[overlayLat objectAtIndex:idx] doubleValue]; coords[1].longitude = [[overlayLong objectAtIndex:idx] doubleValue]; MKPolyline* line = [MKPolyline polylineWithCoordinates:coords count:2]; [line setTitle:[overlayColors objectAtIndex:idx]]; [mapViewGlobal addOverlay:line]; } 

在这里添加全部3000可能需要100ms。 需要很长时间(我认为)的部分是我在第一个方法中创build叠加层的地方。

你想要什么和编译器能做什么之间有一点差距。 当你调用dispatch_async ,你实际上是在告诉CPU“ 在这里,有这么一小块代码,只要你觉得现在就运行,现在不要阻塞我的用户界面线程 ”。 但是,你的方法现在必须返回。 在后台线程中根本没有办法创build任何东西,因为在mapView:viewForOverlay:返回之前,你必须等待它,因为它必须返回一些东西

这种方法不是使用GCD或任何后台代码的地方。 如果你的问题是一次叠加大量的叠加,我会把所有的叠加分成100个块,并在每个批次之间延迟100ms将它们添加到映射中。