Pergunta

When I draw a path with MKOverlays (sometimes with 5000+ individual MKPolylines), there is a very long wait for all of the overlays to be drawn, and every time the map view is scrolled to a new area, the overlays for that area have to be drawn, and there is another noticeable freeze-up.

My dilemma is that I have two sets of code which both draw the path correctly. The first draws the path as one long line, and draws very quickly. The second draws each line segment as an individual line, and takes a long long time.

Now, why would I even consider the second way? Because I have to analyze each individual line to see what color the line should be. For example, if the line's title property is "red", then I make the line red, but if it is "blue", then the line is blue. With the first technique, this kind of specificity is not possible (as far as I know, but maybe someone else knows differently?) because the path is just one big line, and accessing each individual segment is impossible. With the second way it is easy, but just takes a long time.

Here are my two sets of code:

First way (fast but can't access individual segments):

CLLocationCoordinate2D coords[sizeOverlayLat];

for(int iii = 0; iii < sizeOverlayLat; iii++) {
    coords[iii].latitude = [[overlayLat objectAtIndex:iii] doubleValue];
    coords[iii].longitude = [[overlayLong objectAtIndex:iii] doubleValue];
}

MKPolyline* line = [MKPolyline polylineWithCoordinates:coords count:sizeOverlayLat];

[mapViewGlobal addOverlay:line];

Second way (slow but I can draw each line with a specific color):

NSMutableArray* lines = [NSMutableArray new];

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]];
    [lines addObject:line];
}

[mapViewGlobal addOverlays:lines];

My question is: Can I get the performance of the first way with the control over each line that the second way provides me?

Foi útil?

Solução

You can definitely get such performance, but you would probably need to create your own overlay view.

In that view, you can draw polylines by calling CGAddLineToPoint repeatedly, while skipping parts using CGMoveToPoint. Do this separately for each color and you're done. So if you have 2 colors (red+blue), you would loop through your polygon twice, first drawing red (skipping blue pieces) and then drawing blue (skipping red pieces).

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top