0

I developed an application, which uses the map view, which has multiple annotations at the same location, but it is not dropping the pins which I provided the order for the annotations. I added annotations from the array, but it is not showing the same order. I need the first object [annotation] to be the top on the location. when user taps on the annotation, it should show the first object as callout. Any suggestions to do the same.

Thanks I have already tried this approach, but no luck..

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {


for (MKAnnotationView *view in views) {

    if ([[view annotation] isKindOfClass:[Annotation class]]) {
        Annotation *customAnnotation = (Annotation*) [view annotation];
        if ([customAnnotation markerID]==0)
             [[view superview] bringSubviewToFront:view];
        else {
            [[view superview] sendSubviewToBack:view];
        }
    }
}
}
Srinivas G
  • 339
  • 3
  • 16
  • Possible duplicate of http://stackoverflow.com/questions/1145238/how-to-define-the-order-of-overlapping-mkannotationviews/1213450#1213450 – Rayfleck Jan 06 '12 at 15:38

1 Answers1

1

Annotations can put added in an arbitrary order, and can be added or removed from the view the same way cells are in a table view. So the z-layer can vary in a way you might not want.

Here is some sample code that might help you. I have a map that displays pins for search results. They are divided into normal results and favorites. I want the favorites to always be on top or the other pins. I also have a "recticle" to highlight a selected map pin, and I always want this to be on top of all of the annotations. I use this code to get different types of annotations into the right z-layer order:

- (void) mapView:(MKMapView *)aMapView didAddAnnotationViews:(NSArray *)views {

    MKAnnotationView *theReticleView = nil;

    for (MKAnnotationView *view in views) {
        if ([[view annotation] isKindOfClass:[SearchResult class]]) {
            SearchResult *result = (SearchResult*) [view annotation];
            if ([result.isFavorite boolValue])  {
                [[view superview] bringSubviewToFront:view];
            } else {
                [[view superview] sendSubviewToBack:view];
            }
        }
        else if ([[view annotation] isKindOfClass:[MapReticle class]]) {
            theReticleView = view;
        }
    }
    if (theReticleView != nil)
        [[theReticleView superview] bringSubviewToFront:theReticleView];

}
Draco
  • 1,003
  • 1
  • 10
  • 14
  • Hi Draco, Thanks for your quick answer,I have already tried this approach, but no luck. When I tap on the annotation to get the callout, it is showing me other than the first index of my annotations. It should show me the first index of annotations. [One location, has a lot of annotations, it need to show the top one when I tap on the annotation, which is not showing currently.] – Srinivas G Jan 07 '12 at 05:49