问题描述
我需要在所有其他注释之上绘制当前用户注释(蓝点)。现在它正在我的其他注释下面被隐藏起来。我想调整此注释视图的z-index(蓝点)并将其置于顶部,是否有人知道如何?
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?
更新:
所以我现在可以获得MKUserLocationView的处理方法,但是我如何推进呢?
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:????];
}
}
}
推荐答案
在的帮助下,最后使用下面列出的代码使其工作。我遇到的问题是MKUserLocation注释首先被添加到地图之前,所以当你添加其他注释时,它们的顺序似乎是随机的,并且仍然会在MKUserLocation注释之上。为了解决这个问题,我必须将所有其他注释移到后面,并将MKUserLocation注释移到前面。
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];
}
}
}
这篇关于iOS MapKit用户位置注释的Z-index的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!