问题描述
我需要在两点之间绘制路线,并且出于目的使用MKDirectionsRequest
.
I need to draw route between two points and I'm using MKDirectionsRequest
for my purpose.
获取路线是可以的,但是我在绘制它时遇到了麻烦.
Getting a route is OK, but I have trouble with drawing it.
在iOS 8 SDK中没有功能
In iOS 8 SDK there's no function
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
只有一个:
func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer!
由于某种原因,我不明白为什么未调用该方法.
设置了MapView的代理,并导入了MapKit.
And for some reason, I can't understand why that method isn't called.
Delegate for MapView is set and MapKit is imported.
这是实现的rendererForOverlay
函数:
Here is the rendererForOverlay
function that is implemented:
func rendererForOverlay(overlay: MKOverlay!) -> MKOverlayRenderer! {
println("rendererForOverlay");
var overlayRenderer : MKOverlayRenderer = MKOverlayRenderer(overlay: overlay);
var overlayView : MKPolylineRenderer = MKPolylineRenderer(overlay: overlay);
view.backgroundColor = UIColor.blueColor().colorWithAlphaComponent(0.5);
return overlayView;
}
推荐答案
地图视图未调用您的rendererForOverlay
方法,因为其名称不正确.
The map view isn't calling your rendererForOverlay
method because it is not named correctly.
该方法必须准确命名:
mapView(mapView:rendererForOverlay)
但是在您的代码中它被命名为:
but in your code it's named:
rendererForOverlay(overlay:)
另外,您应该检查overlay
参数的类型是否为MKPolyline
并设置折线渲染器的strokeColor
.
In addition, you should check that the type of the overlay
argument is MKPolyline
and set the strokeColor
of the polyline renderer.
(现有代码中的view.backgroundColor
实际上正在更改视图控制器view
的背景颜色-而不是折线.)
(The view.backgroundColor
in the existing code is actually changing the background color of the view controller's view
-- not the polyline.)
示例:
func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
println("rendererForOverlay");
if (overlay is MKPolyline) {
var pr = MKPolylineRenderer(overlay: overlay);
pr.strokeColor = UIColor.blueColor().colorWithAlphaComponent(0.5);
pr.lineWidth = 5;
return pr;
}
return nil
}
这篇关于iOS 8 SDK,Swift,MapKit绘制路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!