【发布时间】:2018-01-26 07:30:58
【问题描述】:
我们如何确保注解视图放置在地图上的指定顺序/位置?
我有一种情况,一个地址可以有多个注释(彼此叠加)。例如,引脚编号 1、2、3 和 4 放置在相同的地理位置。当地图加载时,我希望 Pin #1 位于顶部,但无法确保 Pin #1 始终可见(在顶部)。
环顾四周,我找到了以下解决方案,但没有一个对我有用。
1)。按照您想要的顺序将注释添加到地图中。
根据我的经验,它不起作用!根据我的经验,使用addAnnotations: 将注释添加在一起或将它们一一添加会产生相同的结果。注释视图的顺序仍然是随机的。
[self.mapView addAnnotations:allAnnotations];
生成与以下相同的结果:
for (MyCustomAnnotation *annotation in allAnnotations) {
[self.mapView addAnnotation:annotation];
}
2)。使用bringSubviewToFront 或sendSubviewToBack 指定所有注解的顺序。
根据我的经验,它不起作用!例如,以下代码不会生成所需的结果:
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
// .tag associated with each view specifies the order/hierarchy of annotation views
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"tag" ascending:YES];
NSArray *orderedAnnotationViews = [views sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
for (MKAnnotationView *annotationView in orderedAnnotationViews) {
[mapView sendSubviewToBack:annotationView];
}
}
3)。使用 annotationView.layer.zPosition 指定所有注释的确切顺序。
考虑 4 个引脚重叠的情况,例如引脚 1、2、3 和 4。这种方法的问题在于,点击特定注释引脚或以编程方式选择它不会将其带到前面。 zPosition 是绝对的。您可以在每次选择引脚时更改它,但这并不理想。
zPosition 属性指定图层的 z 轴分量 位置。 zPosition 旨在用于设置视觉 层相对于其兄弟层的位置。不应该 用于指定层兄弟的顺序,而不是重新排序层 在子层数组中。
想法?
【问题讨论】:
-
当您点击它们时,注释的顺序会发生变化。我的意思是对于注释组的每次点击,地图将显示不在顶部的注释。是的,注释的顺序不固定。在这里进行实验的好点。
标签: ios mkmapview mkannotation mkannotationview