I need to draw the current user annotation (the blue dot) on top of all other annotations. Right now it is getting drawn underneath my other annotations and getting hidden. I'd like to adjust the z-index of this annotation view (the blue dot) and bring it to the top, does anyone know how?
Update: So I can now get a handle on the MKUserLocationView, but how do I bring it forward?
- (void) mapView:(MKMapView *)aMapView didAddAnnotationViews:(NSArray *)views { for (MKAnnotationView *view in views) { if ([[view annotation] isKindOfClass:[MKUserLocation class]]) { // How do I bring this view to the front of all other annotations? // [???? bringSubviewToFront:????]; } } }
Overview. Use MapKit to give your app a sense of place with maps and location information. You can use the MapKit framework to: Embed maps directly into your app's windows and views. Add annotations and overlays to a map to call out points of interest.
Finally got it to work using the code listed below thanks to the help from Paul Tiarks. The problem I ran into is that the MKUserLocation annotation gets added to the map first before any others, so when you add the other annotations their order appears to be random and would still end up on top of the MKUserLocation annotation. To fix this I had to move all the other annotations to the back as well as move the MKUserLocation annotation to the front.
- (void) mapView:(MKMapView *)aMapView didAddAnnotationViews:(NSArray *)views { for (MKAnnotationView *view in views) { if ([[view annotation] isKindOfClass:[MKUserLocation class]]) { [[view superview] bringSubviewToFront:view]; } else { [[view superview] sendSubviewToBack:view]; } } }
Update: You may want to add the code below to ensure the blue dot is drawn on top when scrolling it off the viewable area of the map.
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated { for (NSObject *annotation in [mapView annotations]) { if ([annotation isKindOfClass:[MKUserLocation class]]) { NSLog(@"Bring blue location dot to front"); MKAnnotationView *view = [mapView viewForAnnotation:(MKUserLocation *)annotation]; [[view superview] bringSubviewToFront:view]; } } }
Another solution: setup annotation view layer's zPosition (annotationView.layer.zPosition
) in:
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With