I'm trying to use an MKOverlay
(specifically an MKPolyline
) to show a route on the map. However, I need the route to show up above my existing pins (custom MKAnnotationView
s). Is there a good way to bring the MKPolyline
to the front above the pins?

- 623
- 1
- 8
- 33
1 Answers
It might be useful to investigate the annotation layer's zOrder
property. Sending the annotations backwards in your superview's hierarchy may help you achieve the effect needed. Changing your annotation z-axis position should probably be done within the mapView:didAddAnnotationViews:
delegate method.
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
annotationView.layer.zPosition = x;
}
It's been a while since I've dealt with MKPolyline
, and I know things changed a lot in iOS 8, but you might be able to directly manipulate the polyline's z position while you're drawing it.
UPDATE
When you add your MKOverlay
to the map, starting in iOS 7.0+, use the addOverlay:level:
method. This allows you to specify the z-position of the overlay. One of the highest z-positions for the overlay as defined by MKOverlayLevels
is MKOverlayLevelAboveLabels
:
Place the overlay above map labels, shields, or point-of-interest icons but below annotations and 3D projections of buildings.
Unfortunately, as mentioned in the documentation above, the map does not support placing overlays above annotations. A possible solution may be to add the annotations to a separate MKOverlay
object and then to add the other overlay (with the path) to the map using insertOverlay:aboveOverlay:
.

- 8,492
- 12
- 76
- 133
-
zPosition only seems to affect annotations relative to each other - overlays seem to always be behind any annotation – bplattenburg Oct 28 '15 at 18:57
-
Unfortunately, digging further into the documentation, I'm not sure it's possible unless you create the annotations on an overlay (see my update). Seems that Apple has deemed that annotations and overlays need separate realms. Can I ask why you need the path above the annotations? – Sam Spencer Oct 28 '15 at 22:04
-
Hm, that might work. Thanks. Probably not worth the effort at this point though. What I'm trying to do is show a route and unrelated pins at the same time, but the route is more important and should show over the pins. – bplattenburg Oct 29 '15 at 14:59
-
You're right, that would be a lot of work for what you're trying to do. Depending on how you want to approach your UI, it might be easy to have some kind of toggle between the route and the pins so only one shows at a time. Of course, that may not apply to your situation. Good luck on the project! – Sam Spencer Oct 29 '15 at 22:46