How can we ensure that annotation views are placed in a specific order / position on the map?
I have a case where one address can have several annotations (on top of each other). For example, contact numbers 1, 2, 3, and 4 are located in the same geographical location. When the card loads, I want Pin # 1 to be on top, but there is no way to guarantee that Pin # 1 will always be visible (on top).
Looking around, I found the following solutions, but none of them worked for me.
1). Add annotations to the map in the order you want.
In my experience, this does not work! In my experience, adding annotations together with addAnnotations: or adding them one at a time leads to the same result. The order of presentation of annotations is still random.
[self.mapView addAnnotations:allAnnotations]
It produces the same result as:
for (MyCustomAnnotation *annotation in allAnnotations) { [self.mapView addAnnotation:annotation]; }
2). Use the bringSubviewToFront or sendSubviewToBack command to specify the order of all annotations.
In my experience, this will not work! For example, the following code does not generate the required results:
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
3). Use annotationView.layer.zPosition to indicate the exact order of all annotations.
Consider a scenario in which 4 contacts overlap, for example. Contact 1, 2, 3, and 4. The problem with this approach is that clicking on a particular annotation icon or selecting it programmatically does not bring it to the fore. zPosition is absolute. You can change it every time a contact is selected, but this is not ideal.
The zPosition property indicates the component of the z-axis of the position layer. The purpose of zPosition is to set the visual position of a layer relative to its related layers. This should not be used to indicate the order of siblings, instead reorders the layer in the sublayer array.
Thoughts?