IOS 8 SDK, Swift, MapKit Drawing route
I need to draw a route between two points and I am using it MKDirectionsRequest
for my purpose.
Getting the route is fine, but I'm having trouble drawing it.
IOS 8 SDK has no feature
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
There is only one:
func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer!
And for some reason I can't figure out why this method is not being called.
The delegate for MapView is installed and MapKit is imported.
Here is the implemented function rendererForOverlay
:
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;
}
source to share
The map view does not call your method rendererForOverlay
because it is incorrectly specified.
The method must be named exactly:
mapView(mapView:rendererForOverlay)
but in your code, it called:
rendererForOverlay(overlay:)
Also, you should check that the argument type overlay
is equal MKPolyline
and set strokeColor
the polyline renderers.
( view.backgroundColor
in existing code actually changes the background color of the view controller view
, not the polyline.)
Example:
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
}
source to share