First, instead of MKPolylineView
, you must create an MKPolylineRenderer
.
MKPolylineView
has been deprecated since iOS 7 and, although you can still use it in Objective-C if necessary, it's not supported in Swift.
Second, you must create and return an MKPolylineRenderer
in the rendererForOverlay
delegate method (not pass it to addOverlay
).
In the addOverlay
method, you pass the MKPolyline
object (not the MKPolylineView
or MKPolylineRenderer
).
(See Adding MKOverlayPathRenderer as overlay to MKMapView gets exception for an explanation of the difference between what object you pass to addOverlay
vs. what object you return in rendererForOverlay
.)
So in the setMapView
method, remove the lines that create and set myPolylineView
and change the addOverlay
line to:
self.theMapView.addOverlay(polyline)
Then implement the rendererForOverlay
method:
func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
if overlay is MKPolyline {
var polylineRenderer = MKPolylineRenderer(overlay: overlay)
polylineRenderer.strokeColor = UIColor.blueColor()
polylineRenderer.lineWidth = 5
return polylineRenderer
}
return nil
}
Make sure the map view's delegate
is set otherwise the delegate method won't get called and the overlay will not appear. If theMapView
is an IBOutlet, connect the delegate
outlet or set it in code (eg. in viewDidLoad
after calling super):
self.theMapView.delegate = self
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…