如何在MKMapView上使用MKOverlayView绘制圆弧/曲线
现在需要帮助。 我可以使用 MKPolyline 和 MKPolylineView 绘制线条,但是如何在 MKMapView 上的两个坐标之间绘制圆弧或曲线? 非常感谢。
Help needed now.
I can draw lines with MKPolyline and MKPolylineView, but how to draw an arc or curve lines between two coordinates on the MKMapView?
Great thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在回答问题之前,重要的是要提到 MKOverlayView 已弃用,并且从 iOS7 及更高版本开始我们应该使用 MKOverlayRenderer:
现在我们可以分解如何实现圆弧/曲线:
MKMapView
将要求我们提供一个与我们已经创建的MKPolyline
相对应的适当的MKOverlayRenderer
在第 1 节中创建。我们需要的 方法 是:mapView(_mapView: MKMapView, rendererFor 覆盖: MKOverlay) -> MKOverlayRenderer
基本上:
MKOverlayPathRenderer
子类化,它显然继承自MKOverlayRenderer
并且如文档所述:因此,如果我们按原样使用新的子类对象,我们将得到一个开箱即用的解决方案,它是从第一个坐标到我们在第 1 节中定义的第二个坐标的实线,但由于我们想要一个曲线我们必须重写一个方法:
这意味着在我们的
CustomObject: MKOverlayPathRenderer
中,我们将重写createPath
:我们基本上完成了。您可能需要考虑添加宽度/描边/颜色等,以便您可以看到曲线。
4.1。重写
override func draw(_ mapRect: MKMapRect, ZoomScale: MKZoomScale, in context: CGContext)
后,在添加渐变之前,您必须添加第 3 节中的相同代码,但不要更改在内部path
中,您必须将其添加到提供的上下文中:这基本上为我们提供了一条关于渐变着色所需的曲线。
4.2.我们需要使用
path.boundingBox
而不是使用path.boundingBoxOfPath
,因为:与
boundingBoxOfPath
不同:希望有帮助:)
Before answering the question it is important to mention that MKOverlayView is deprecated and from iOS7 and later we should use MKOverlayRenderer:
We can now break it down on how to implement the arc/curve line:
MKMapView
will want us to provide a properMKOverlayRenderer
in corresponding to theMKPolyline
we've created at section 1. The method we need is:mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer
Which basically:
MKOverlayPathRenderer
which obviously inherits fromMKOverlayRenderer
and as documentation states:So if we will use our newly subclass object as is, we will get an out-of-the-box solution which is a solid line from the 1st coordinate to the 2nd one we've defined in section 1, but since we want a curved line we will have to override a method for that:
It means that inside our
CustomObject: MKOverlayPathRenderer
we will overridecreatePath
:We are basically done. You might want to consider adding width/stroke/color etc so you could see the curved line.
4.1. After overriding
override func draw(_ mapRect: MKMapRect, zoomScale: MKZoomScale, in context: CGContext)
, just before adding the gradient, you will have to add the same code from section 3 but instead of changing the innerpath
you will have to add it to the provided context:This basically gives us a curved line we need regarding the gradient coloring.
4.2. Instead of using
path.boundingBoxOfPath
we will need to usepath.boundingBox
because:Unlike
boundingBoxOfPath
:Hope that helps :)
阅读文档后,您似乎可以创建 MKOverlayPathView 实例并将任意 CGPathRef 对象分配给其 path 属性。该路径可以包含直线和圆弧。
我不知道(并且文档没有提到)路径的坐标应该采用什么格式(
MKMapPoint
或CLLocationCooperative2D
浮现在脑海中),但你可能可以通过尝试一下就能找到答案。Reading the documentation, it seems that you can create an instance of
MKOverlayPathView
and assign an arbitraryCGPathRef
object to itspath
property. This path can contain both straight lines and arcs.I don't know (and the documentation doesn't mention) in what format the coordinates of the path should be (
MKMapPoint
orCLLocationCoordinate2D
come to mind) but you can probably find that out by experimenting a bit.您已经在评论中回答了自己,但我想向大家指出优秀的 AIMapViewWrapper 项目GitHub,其中包含用于绘制圆弧的示例代码一组坐标上的路径。在该项目中,它用于绘制飞机所采用的路径,包括其下方的阴影路径(以及其他内容,例如沿该路径制作飞机动画)。对于任何对此感兴趣的人来说应该派上用场。
You answered yourself in your comment already, but I'd like to point everyone at the excellent AIMapViewWrapper project on GitHub which includes sample code for plotting an arc path over a set of coordinates. In that project it's being used to draw the path a plane takes including a shadow path underneath it (and other stuff such as animating a plane along that path). Should come in handy for anyone taking a stab at this.
如果你想:
所以解决方案是:
这里还有一些提示:
If you want to:
So the solutions are:
And some tips here: