【发布时间】:2017-09-20 19:47:54
【问题描述】:
我有一个 TableView,用于在点击单元格时显示 MapView 注释标注。
在 iOS 10 中,我可以将 MapView 集中在注释上,然后使用以下命令显示它的标注:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let location = locations[indexPath.item]
mapView.setCenter(location.coordinate, animated: true)
mapView.selectAnnotation(location, animated: true)
}
locations 是 MKAnnotations 的数组。我在 iOS 10 上使用 MKPinAnnotationViews,在 iOS 11 上使用 MKMarkerAnnotationViews。
iOS 11 会在您缩放地图时自动隐藏和显示 MKMarkerAnnotationViews。
这具有阻止.selectAnnotation() 可靠工作的不幸副作用,因为标记在地图居中后仍可能被隐藏。
我已查看文档并理解原因:
如果指定的注释不在屏幕上,因此不会 有关联的注解视图,此方法无效。
有没有办法禁用注释集群/隐藏? 或者有什么方法可以强制选中的注解可见?
【问题讨论】:
-
这不是一个解决方案,但可能是一个解决方法的想法:尝试以编程方式将地图缩放到更放大的状态(您可能可以使用缩放因子来获得它,以便注释不重叠)在您将地图居中后。
-
嗨@aksh1t 我考虑过这个,但我不想弄乱用户选择的缩放级别。另外,我的一些注释可能非常接近,因此在某些情况下这实际上可能不起作用。
-
这是有道理的。这是我在查看其他一些 mapview 方法时得到的另一个想法:尝试使用您的注释调用 showAnnotations: 方法,然后执行
selectAnnotation。 (我不知道 showAnnotation 方法是否改变了缩放级别;它可能确实改变了缩放级别)。 -
showAnnotations确实会影响缩放。
标签: ios swift mapkit mkannotation ios11