更改已添加到地图视图的 MKOverlay 的颜色

发布于 2024-11-14 10:38:40 字数 162 浏览 2 评论 0原文

我有一些 MKOverlays(实际上它们是 MKPolygons),地图一显示就会加载。我想动态改变它们的颜色。我能看到的唯一方法是删除覆盖层,然后用新颜色将其添加回来。有没有更好的方法在现有的覆盖上执行此操作?

我是 Objective-C/xcode/ios 的新手...所以请温柔点:)

I have some MKOverlays(actually they are MKPolygons) that are loaded as soon as the map shows up. I would like to change their color dynamically. The only way I can see of doing this, is to remove the overlay then add it back with the new color. Is there a better way to do this on the existing overlay?

I am brand new at objective-c/xcode/ios ... so please be gentle :)

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

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

发布评论

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

评论(2

梦境 2024-11-21 10:38:40

你的mapView有一个方法来获取给定覆盖层的渲染器对象。然后,您可以使用渲染器更改叠加层的颜色。

if let renderer = mapView.rendererForOverlay(overlay) as? MKPolygonRenderer {
    renderer.fillColor = UIColor.redColor()
}

如果您不寻找 MKPolygon 叠加层,请忽略对 MKPolygonRenderer 的可选转换。

(我意识到这是一个相当老的问题,但我只是偶然发现它并找到了我的解决方案

Your mapView has a method for getting the renderer object for a given overlay. You can then use the renderer to change the color of your overlay.

if let renderer = mapView.rendererForOverlay(overlay) as? MKPolygonRenderer {
    renderer.fillColor = UIColor.redColor()
}

Leave out the optional cast to MKPolygonRenderer if you're not looking for an MKPolygon overlay.

(I realize this is a rather old question, but I just stumbled across it and found my solution ????)

十年九夏 2024-11-21 10:38:40

重要的是要记住,MapKit 的大部分都有不同的对象(MKPolygon、MKCircle、MKShape)来保存与绘制视图相关的数据(MKPolygonView、MKCircleView、MKOverlayView 等)。在许多情况下,您希望获得对视图的引用对象,以便您可以设置背景颜色。即,

MKOverlayView *anOverlay;  //You need to set this view to the object you are interested in
anOverlay.backgroundColor = [UIColor redColor]; 
[anOverlay setNeedsDisplay];

如果您的对象是 MKPolygon,您应该确定它被绘制到的 MKPolygonView,然后设置 fillColor 属性并通过调用 setNeedsDisplay 重绘对象:

MKPolygonView *theView;
theView.fillColor = [UIColor redColor];
[theView setNeedsDisplay];

It is important to remember that much of MapKit has different objects (MKPolygon, MKCircle, MKShape) to hold the data related to drawing a view (MKPolygonView, MKCircleView, MKOverlayView, etc.) In many cases you want to get a reference to the view object so you can then set the background color. i.e.

MKOverlayView *anOverlay;  //You need to set this view to the object you are interested in
anOverlay.backgroundColor = [UIColor redColor]; 
[anOverlay setNeedsDisplay];

If your object is an MKPolygon, you should determine the MKPolygonView it is being drawn into an then set the fillColor property and redraw the object by calling setNeedsDisplay:

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