绘制许多 MKOverlay 时的性能问题

发布于 2025-01-06 14:07:57 字数 1652 浏览 1 评论 0原文

当我使用 MKOverlays 绘制路径时(有时使用 5000 多个单独的 MKPolylines),需要等待很长时间才能绘制所有叠加层,并且每次地图视图滚动到新区域时,该区域的叠加层都会被绘制,并且还有另一个明显的冻结。

我的困境是我有两组代码,它们都正确地绘制了路径。第一个将路径绘制为一条长线,并且绘制得非常快。第二个将每个线段绘制为一条单独的线,并且需要很长很长的时间。

现在,为什么我还要考虑第二种方式呢?因为我必须分析每一条线,看看该线应该是什么颜色。例如,如果该行的 title 属性是“red”,那么我将该行设为红色,但如果它是“blue”,则该行为蓝色。使用第一种技术,这种特殊性是不可能的(据我所知,但也许其他人知道不同?),因为路径只是一条大线,并且访问每个单独的段是不可能的。第二种方法很简单,但需要很长时间。

这是我的两组代码:

第一种方式(快速但无法访问各个段):

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];

第二种方式(缓慢但我可以用特定颜色绘制每条线): >

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];

我的问题是:我能否通过第二种方式提供的对每一行的控制来获得第一种方式的性能?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怼怹恏 2025-01-13 14:07:57

您绝对可以获得这样的性能,但您可能需要创建自己的覆盖视图。

在该视图中,您可以通过重复调用CGAddLineToPoint来绘制折线,同时使用CGMoveToPoint跳过部分。对每种颜色分别执行此操作,就完成了。因此,如果您有两种颜色(红色+蓝色),您将循环遍历多边形两次,首先绘制红色(跳过蓝色部分),然后绘制蓝色(跳过红色部分)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文