How do I show MKOverlay above MKAnnotations?
Asked Answered
C

1

7

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 MKAnnotationViews). Is there a good way to bring the MKPolyline to the front above the pins?

Clothespress answered 20/10, 2015 at 15:34 Comment(0)
I
5

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:.

Indra answered 27/10, 2015 at 23:5 Comment(4)
zPosition only seems to affect annotations relative to each other - overlays seem to always be behind any annotationClothespress
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?Indra
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.Clothespress
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!Indra

© 2022 - 2024 — McMap. All rights reserved.