I want to customize the lines drawn on MKMapView to show a route so that the lines have a border color and a fill color. Similar to this where it has a black border and is filled with another color:
I'm currently just returning MKPolyLineView objects from mapView:viewForOverlay:
which works fine for plain lines. The docs says the MKPolyLineView is not to be subclassed, so should I subclass MKOverlayView and implement my own drawMapRect? Or should I subclass MKOverlayPathView? Or create a replacement for MKPolylineView?
EDIT - what I'm asking is: where is the place to put your own Quartz drawing code in order to draw your own annotations/overlays? Currently I've created a subclass of MKOverlayView and implement my own drawMapRect:zoomScale:inContext: It's pretty easy to draw the overlay that way but is that the best solution?
I use a subclass NamedOverlay that holds an overlay an a name:
NamedOverlay.h
NamedOverlay.m
and in the map controller I instantiate two overlays with different name, then in the
MKMapViewDelegate
I can identify which overlay I want to draw and do something like:I know that this may not match the pure approach you want, but why not using
MKPolygon
instead of aMKPolyLine
?Create a
MKPolygon
instance that represents a kind of corridor around your route, and then , when you create theMKPolygonView
that corresponds to the MKPolygon/corridor you've created, set the properties of theMKPolygonView
to get a different fill color and strokeColorI didn't try it myself but this should work. Only drawback is that when you zoom in / out, the 'width' of the 'route' will change.... :/
MKPolylineView
can only be used for stroking a designated path. You can use some of the properties inMKOverlayPathView
to change their appearance but only some of them would apply, e.g.fillColor
,strokeColor
.If you want to draw something more complex, you can use
MKOverlayPathView
. It is more generic and thus suited for more than just stroking paths. For drawing simple lines, the result would be identical toMKPolylineView
(at least, according to the docs).If you want to do more complex drawing, subclass
MKOverlayPathView
. What you're trying to do is non-trivial.You can do this by implementing your own MKOverlayPathView subclass, which draws the path twice in the map rect. Once thicker with black and once thinner on top with another colour.
I have created a simple drop-in replacement of MKPolylineView which lets you do that: ASPolylineView.
If you want to do it yourself, the two main methods that you need to implement could look like this:
You can just add two MKPolyLineView objects with the same coordinates, but different thicknesses.
Add one with a lineWidth of 10 (or whatever) with strokeColor set to black.
Then add another with a lineWidth of 6 with strokeColor set to your other desired color.
You can use the same MKPolyLine for both MKPolyLineView objects.