Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Z-index of iOS MapKit user location annotation

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:????];         }     } } 
like image 549
jmcopeland Avatar asked Aug 22 '11 01:08

jmcopeland


People also ask

What is MapKit in IOS?

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.


2 Answers

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];     }   } } 
like image 195
jmcopeland Avatar answered Oct 18 '22 14:10

jmcopeland


Another solution: setup annotation view layer's zPosition (annotationView.layer.zPosition) in:

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

like image 41
hrchen Avatar answered Oct 18 '22 16:10

hrchen