iOS MapKit用户位置注释的Z索引

我需要在所有其他注释之上绘制当前用户注释(蓝点)。 现在它正在我的其他注释下面被吸引,并被隐藏起来。 我想调整这个注解视图(蓝点)的z-index并将其带到最高处,有谁知道如何?

更新:所以我现在可以得到一个MKUserLocationView的句柄,但我该如何把它呢?

- (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:????]; } } } 

最后得到它的工作使用下面列出的代码感谢保罗Tiarks的帮助。 我碰到的问题是,MKUserLocation注解先于其他注释被添加到地图中,所以当添加其他注释时,它们的顺序看起来是随机的,并且仍然会在MKUserLocation注释之上。 为了解决这个问题,我不得不将所有其他注释移动到后面,并将MKUserLocation注释移到最前面。

 - (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]; } } } 

更新 :您可能需要添加下面的代码,以确保将蓝点从地图的可视区域滚动到顶部时在顶部绘制。

 - (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]; } } } 

另一个解决scheme:在下面设置注释视图图层的zPosition( annotationView.layer.zPosition

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

该线程的官方答案是错误的…使用zPosition确实是最好的办法和最快的vs使用regionDidChangeAnimated …

否则你会在地图上的许多注释遭受巨大的性能影响(因为每一个帧的变化将重新扫描所有的注释)。 并一直在testing它…

所以当创build注释的视图(或在didAddAnnotationViews)中设置:self.layer.zPosition = -1; (低于所有其他)

正如yuf所指出的:这使得封面从其他引脚标出 – – yuf 13年12月5日在20:25

即注释视图将出现在其他引脚的下面。

要解决这个问题,当你有一个select时,简单地把zPosition命名为0

 -(void) mapView:(MKMapView*)mapView didSelectAnnotationView:(MKAnnotationView*)view { if ([view isKindOfClass:[MyCustomAnnotationView class]]) view.layer.zPosition = 0; ... } -(void) mapView:(MKMapView*)mapView didDeselectAnnotationView:(MKAnnotationView*)view { if ([view isKindOfClass:[MyCustomAnnotationView class]]) view.layer.zPosition = -1; ... } 

尝试获取用户位置注释的引用(可能在mapView: didAddAnnotationViews: ),然后在添加了所有注释之后,将该视图放到mapView的前面。

Swift 3:

 internal func mapView(_ mapView: MKMapView, didAdd views: [MKAnnotationView]) { for annotationView in views { if annotationView.annotation is MKUserLocation { annotationView.bringSubview(toFront: view) return } annotationView.sendSubview(toBack: view) } } 

这是一种使用谓词来完成的方法。 我认为它应该更快

 NSPredicate *userLocationPredicate = [NSPredicate predicateWithFormat:@"class == %@", [MKUserLocation class]]; NSArray* userLocation = [[self.mapView annotations] filteredArrayUsingPredicate:userLocationPredicate]; if([userLocation count]) { NSLog(@"Bring blue location dot to front"); MKAnnotationView *view = [self.mapView viewForAnnotation:(MKUserLocation *)[userLocation objectAtIndex:0]]; [[view superview] bringSubviewToFront:view]; } 

使用Underscore.m

 _.array(mapView.annotations). filter(^ BOOL (id<MKAnnotation> annotation) { return [annotation isKindOfClass:[MKUserLocation class]]; }) .each(^ (id<MKAnnotation> annotation) { [[[mapView viewForAnnotation:annotation] superview] bringSubviewToFront:[mapView viewForAnnotation:annotation]]; });