如何创建图像叠加层并将其添加到 MKMapView?
我正在尝试学习 MapKit,现在正在尝试将图像作为叠加层添加到地图视图中。 我似乎找不到任何示例代码来帮助解释如何执行此操作。
你们能帮助我如何创建 MKOverlay
并将它们添加到 MKMapKit
中吗?
提前致谢..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以下是如何将
UIImage
设置为MKMapView
叠加层的示例。一些参数(坐标和图像路径)是固定的,但我猜代码可以很容易地改变。创建一个符合
MKOverlay
的类:MapOverlay.h
MapOverlay.m
创建一个符合 MKOverlayView 的类:
MapOverlayView.h
MapOverlayView.m
将叠加层添加到 MKMapView:
在 mapView 委托上,实现 viewForOverlay :
希望有帮助!
Here is an example to how to sets a
UIImage
to aMKMapView
overlay. Some parameter (coordinates and image path) are fixed, but the code can be easily changed, I guess.Create an class that conforms to
MKOverlay
:MapOverlay.h
MapOverlay.m
Create an class that conforms to MKOverlayView:
MapOverlayView.h
MapOverlayView.m
Add the overlay to the MKMapView:
On the mapView delegate, implement the viewForOverlay:
Hope it helps!
文档:
示例代码:
Documentation:
Sample code:
在寻找问题的解决方案时,我不断回到这个问题,这里 还有这里
他们都是回答我需要回答的问题,但相反。
我需要确定 UIView(即 MapView 上的绘制方块)是否包含地图图钉,如果它确实让我知道,以便我可以删除它或选择它等。
最后,在尝试将我的 UIView 转换为某些 MKCooperativeRegion 或类似内容之后,我思想工作倒退。我没有将 UIView 或正方形变成疯狂的地图,而是将图钉变成简单的 CGPoints 并做了一个简单的 Rect contains point。这最终成为我的解决方案..大约 2 小时后!
希望这可以帮助以后的人
In the search for a solution for my problem, I keep coming back to this question, and here and also here
They were all answering the question I needed answered but in reverse.
I needed to determine if a UIView, i.e. a draw square on my MapView contains a map pin, if it does let me know so I can remove it or select it etc.
Finally after trying to convert my UIView into some MKCoordinateRegion or alike, I thought work backwards. Instead of turning my UIView or square into map madness, I turned the pins into simple CGPoints and did a simple Rect contains point. Which ended up being my solution.. after about 2 hrs!
Hope this help someone down the line
只需查看Apple的文档
MKOverlay
协议,看起来不是很复杂。只需创建一个类来执行计算覆盖所需的任何操作,并确保它符合该协议。这意味着您必须具有coefficient
和boundingMapRect
属性以及intersectsMapRect:
的实现。苹果文档解释了这一切。看看 HazardMap 来自 Apple 的示例代码。它使用自定义叠加视图,就像您想要在叠加中显示图像一样。您只需在
MKMapViewDelegate
类中实现mapView:viewForOverlay:
并返回MKOverlayView
子类的新实例即可绘制自定义叠加层在屏幕上。From just looking at Apple's documentation for the
MKOverlay
protocol, it doesn't look very complex. Just make a class to do whatever you need to compute the overlay and make sure it conforms to that protocol. That means you must have thecoordinate
andboundingMapRect
properties as well as an implementation forintersectsMapRect:
. The Apple docs explain it all.Take a look at the HazardMap sample code from Apple. It uses a custom overlay view like you want to do to display an image in the overlay. You simply need to implement
mapView:viewForOverlay:
in yourMKMapViewDelegate
class and return a new instance of your subclass ofMKOverlayView
to get the custom overlay drawn on the screen.