更改已添加到地图视图的 MKOverlay 的颜色
我有一些 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的mapView有一个方法来获取给定覆盖层的渲染器对象。然后,您可以使用渲染器更改叠加层的颜色。
如果您不寻找 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.
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 ????)
重要的是要记住,MapKit 的大部分都有不同的对象(MKPolygon、MKCircle、MKShape)来保存与绘制视图相关的数据(MKPolygonView、MKCircleView、MKOverlayView 等)。在许多情况下,您希望获得对视图的引用对象,以便您可以设置背景颜色。即,
如果您的对象是 MKPolygon,您应该确定它被绘制到的 MKPolygonView,然后设置 fillColor 属性并通过调用 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.
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: