I need some code which can be used outside of the mk* functions. I need to run my custom function to bring the FIRST and LAST markers in an array to the front. (so on top of all the other markers on my screen). I have tried
[self.view bringSubviewToFront:[[mapView annotations]objectAtIndex: 0]];
I have used [[mapView annotations]objectAtIndex: 0]
in my code and that works, but it crashes when I try to bring this to the front. Am I accessing the wrong layer or something?
thanks for any help.
You're bringing the wrong things to the front. Namely, the
annotations
array is an array of objects that conform to theMKAnnotation
protocol (so of typeid<MKAnnotation>
), and they are not views.Instead, you should be getting the view for the annotations you want, and bring those to the front:
However, you should note a couple of things:
annotationView
may benil
if the annotation is on a point that's off screen, or if the annotations haven't finished being added to the map. However, if it isnil
, you probably don't care if an annotation that isn't even on screen is in front or not.bringSubviewToFront
on theannotationView
's superview, and not onself.view
or evenmapView
, as neither of those are the superview of theannotationView
.This worked much better for me:
setup annotation view layer's zPosition (annotationView.layer.zPosition) in:
Note that I assume the default zPosition is 0. Setting to 3 made all the 3 markers show up on top for me.