views:

29

answers:

1

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.

A: 

You're bringing the wrong things to the front. Namely, the annotations array is an array of objects that conform to the MKAnnotation protocol (so of type id<MKAnnotation>), and they are not views.

Instead, you should be getting the view for the annotations you want, and bring those to the front:

id<MKAnnotation> annotation = [[mapView annotations] objectAtIndex:0];
MKAnnotationView* annotationView = [mapView viewForAnnotation:annotation];
if (annotationView != nil) {
    [annotationView.superview bringSubviewToFront:annotationView];
}

However, you should note a couple of things:

  1. annotationView may be nil 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 is nil, you probably don't care if an annotation that isn't even on screen is in front or not.
  2. You need to call bringSubviewToFront on the annotationView's superview, and not on self.view or even mapView, as neither of those are the superview of the annotationView.
Itay
that makes sense - thank you! I need not worry about the annotation being nil as previous zoom to region code ensures that when I call this code they will be on screen (at least for a fraction - as they are drawn). I'll give the code a whirl tomorrow. Many thanks for the reply.
Matt Facer