- objective-c - iOS 5 : Can you override UIAppearance customisations in specific classes?
- iphone - 如何将 CGFontRef 转换为 UIFont?
- ios - 以编程方式关闭标记的信息窗口 google maps iOS
- ios - Xcode 5 - 尝试验证存档时出现 "No application records were found"
现在需要帮助。我可以用MKPolyline和MKPolylineView画线,但是如何在MKMapView上的两个坐标之间画弧线或曲线呢?非常感谢。
最佳答案
在回答问题之前,重要的是要提到 MKOverlayView已弃用,从 iOS7 开始我们应该使用 MKOverlayRenderer :
In iOS 7 and later, use the MKOverlayRenderer class to display overlays instead.
我们现在可以分解如何实现圆弧/曲线:
let polyline = MKPolyline(coordinates: coordinates, count: coordinates.count)
mapView.addOverlay(polyline)
MKMapView
需要我们提供一个合适的 MKOverlayRenderer
对应于我们在第 1 部分创建的 MKPolyline
. method我们需要的是:mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer
基本上:
Asks the delegate for a renderer object to use when drawing the specified overlay.
MKOverlayPathRenderer
进行子类化,它显然继承自 MKOverlayRenderer
并且如文档所述:Use this renderer when your overlay's shape is defined by a CGPath object. By default, this renderer fills the overlay's shape and represents the strokes of the path using its current attributes.
因此,如果我们按原样使用新的子类对象,我们将得到一个开箱即用的解决方案,它是从我们在第 1 节中定义的第一个坐标到第二个坐标的实线,但是由于我们想要一条曲线,我们必须为此覆盖一个方法:
You can use this class as-is or subclass to define additional drawing behaviors. If you subclass, override the createPath() method and use that method to build the appropriate path object. To change the path, invalidate it and recreate the path using whatever new data your subclass has obtained.
这意味着在我们的 CustomObject: MKOverlayPathRenderer
中,我们将覆盖 createPath
:
override func createPath() {
let polyline = overlay as! MKPolyline
// Getting the coordinates from the polyline
let points = polyline.points()
// Taking the center of the polyline (between the 2 coordiantes) and converting to CGPoint
let centerMapPoint = MKMapPoint(polyline.coordinate)
// Converting coordinates to CGPoint corresponding to the specified point on the map
let startPoint = point(for: points[0])
let endPoint = point(for: points[1])
let centerPoint = point(for: centerMapPoint)
// I would like to thank a co-worker of mine for the controlPoint formula :)
let controlPoint = CGPoint(x: centerPoint.x + (startPoint.y - endPoint.y) / 3,
y: centerPoint.y + (endPoint.x - startPoint.x) / 3)
// Defining our new curved path using Bezier path
let myPath = UIBezierPath()
myPath.move(to: startPoint)
myPath.addQuadCurve(to: endPoint,
controlPoint: controlPoint)
// Mutates the solid line with our curved one
path = myPath.cgPath
}
我们基本上完成了。您可能需要考虑添加宽度/描边/颜色等,这样您可以看到曲线。
4.1。覆盖 override func draw(_ mapRect: MKMapRect, zoomScale: MKZoomScale, in context: CGContext)
之后,就在添加渐变之前,您必须添加第 3 节中的相同代码,但不要更改内部 path
您必须将其添加到提供的上下文中:
context.move(to: startPoint)
context.addQuadCurve(to: endPoint,
control: controlPoint)
这基本上为我们提供了有关渐变着色所需的曲线。
4.2。我们需要使用 path.boundingBox
而不是使用 path.boundingBoxOfPath
因为:
... including control points for Bézier and quadratic curves.
不同于boundingBoxOfPath
:
... not including control points for Bézier and quadratic curves.
希望对您有所帮助:)
关于iphone - 如何在MKMapView上用MKOverlayView绘制圆弧/曲线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5673303/
在我当前设计的应用程序中,我有一个带有覆盖层的 MKMapView (顺便说一句,自定义的 MKPolylines),我希望能够检测到触摸事件这些覆盖层并为每个覆盖层分配特定的操作。有人可以帮我解决这
我在我的 .xib 上放置了一个 MKMapView。 它工作正常,但我需要 map 根据罗盘方向旋转? 我读过有关“指南针模式”的内容。 也许我需要将 compassMode 的 MKMapView
我有一个带有注册委托(delegate)的 MKMapView,因此我可以监听区域更改事件(具体来说,regionDidChangeAnimated)。我正在寻找一种可靠的方法来判断区域更改事件是用户
我正在尝试将 MKAnnotationView 添加到 MKMapView 但我做不到……有人能帮我吗? 这是我的代码: override func viewDidLoad() { super
我想像照片应用程序中那样隐藏和显示导航栏但不会丢失 MKMapView 的功能。用户仍然应该能够双击进行缩放、捏合和缩放,并能够选择注释。 我试过: UITapGestureRecognizer*
我在 MKMapView 上有一个 MKCircle。它是用户可拖动的,但如果用户拖动圆圈外的区域, map 应该会移动。 - (IBAction)createPanGestureRecognizer
我正在开发一款与 map 配合使用的 iPhone 应用程序。在此应用程序中,我需要根据纬度和经度显示一些 parking 位(地址)。至此,我成功完成了,但我想显示相对于用户方向(SE、SW、NE、
如何比较 MKMapView 的当前跨度?我正在使用以下代码: - (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)a
我们在我们的一个应用程序中看到了意外行为 - 我们在一个屏幕上显示 map View 上的注释,用户可以通过单击按钮更改显示的注释。 当我们用 iOS7 重建应用程序时,屏幕会定期卡住,即一旦下面的代
我正在尝试创建一个带有 MKMapView 和一个导航到用户位置的按钮的 SwiftUI View 。以下是我的全部代码。我创建了一个名为 MapView 的 View ,它符合 UIViewRepr
我的代码在iOS 7到8上运行良好。昨天进行了更新,我的图钉上的自定义图像已替换为标准的图钉图像。 有什么建议? 我的代码: extension ViewController: MKMapViewDe
我可以使用 MKMApView 组件创建一个 iOS 商业应用程序(在应用程序商店中花费几美元下载)吗? 我知道它是基于 Google 图片,所以...我不知道。 最佳答案 当然,MapKit 已用于
我有用户位置和边界。边界有8个坐标。我需要检查用户是否在边界内。我正在 iPhone 中使用 MapView。 提前致谢。 最佳答案 使用 Map Kit 中的一些内置函数来执行此操作的一种方法是将边
我有一个 MkMapView,并希望将 map 保持在用户位置的中心,就像按下范围按钮时 iPhone map 应用程序所做的那样。 我使用 setCenterCoordinate 方法,因为我不需要
浏览了这里的一些问题后,我对缩放级别和 MKMapView 有了一个大概的了解。然后我发现了这个很棒的博客: http://troybrant.net/blog/2010/01/set-the-zoo
我有一个使用以下代码删除 pin 的功能: ParkPlaceMark *placemark=[[ParkPlaceMark alloc] initWithCoordinate:location];
双击 MKMapView 时:放大。 但是如何缩小呢? 最佳答案 总是使用两根手指来放大和缩小。在模拟器上,您需要按住选项键才能在模拟屏幕上显示“两根手指”(我认为这是 Alt 键,但我在 Mac 上
释放 MKMapView 后,我遇到了奇怪的(?)崩溃。 MKMapView 是我的 View Controller 中的 subview ,在我从导航堆栈中删除该 View 并释放它后,应用程序崩溃
我通过单击按钮缩小 map 。 因此,本地图完全缩小时,如果我尝试再次缩小它,那么它在设置区域时会崩溃。 不确定,但是,有没有办法检测 map 是否达到最大缩放限制? 这是我缩小 map 的代码 -(
我需要获得某种比例因子,它可以告诉我 map 缩放了多少(每当区域发生变化时)。 我的第一个想法是使用跨度的纬度或经度增量值。我会跟踪“旧”值,然后在 map 区域发生变化时更新该值,并将比率与旧值进
我是一名优秀的程序员,十分优秀!