问题描述
我有一个带有几个注释的 MKMapView
,其中三个注释彼此非常接近.
I have a MKMapView
with several annotations and 3 of them are very close to each other.
我使用了 mapView.showAnnotations(mapView.annotations,animation:false)
在启动时在同一区域上显示所有注释,但由于距离太近,三个注释之一被隐藏了.
I used mapView.showAnnotations(mapView.annotations, animated: false)
to display all annotations on the same region at launch but one of the 3 is hidden because it's too close.
我查看了Apple的文档,但找不到防止这种情况发生的方法,知道如何防止注释分组吗?
I looked into Apple's Documentation but couldn't find a way to prevent this from happening, any idea how to prevent annotations grouping?
(我以前从未见过,也许这是iOS 11的功能)
(I've never seen this before, maybe it's a iOS 11 feature)
推荐答案
你看到的不是聚类(你必须编写代码才能得到一个聚类,而你通常会看到一个聚类)
What you are seeing ist not clustering (you had to write code to get a cluster and you would usually see a cluster)
我的实验似乎表明...
My experiments appear to suggest...
-
MKAnnotationViews
从地图的顶部到底部呈现.(北方在哪里都没关系.) - 如果
MapKit
决定绘制重叠的MKAnnotationViews
,那么靠近底部的MKAnnotationView
将绘制在顶部(因为它是稍后绘制的) - 不仅
MKAnnotationViews
,在MKMArkerAnnotationViews
下呈现的标题也需要空间.这些标题的呈现受markerView.titleVisibility
的影响.如果将markerView.titleVisibility
设置为.visible
(而不是默认的.adaptive
),则此标题要比MarkerAnnotationView强
,即使稍后的MarkerAnnotationView
具有displayPriority = .required
.靠近底部的MarkerAnnotationView
不会呈现. - 即使靠近顶部的
MarkerAnnotationView
具有较低的displayPriority
,也会发生这种情况.因此,displayPriority
和.titleVisibility = .visible
较低的MarkerAnnotationView
可以使MarkerAnnotationView
靠近底部,而displayPriority = .required
消失.
MKAnnotationViews
are rendered from top to bottom of the map. (It doesn't matter where north is).- If
MapKit
decides to draw overlappingMKAnnotationViews
, then theMKAnnotationView
nearer to the bottom is drawn on top (because it's drawn later) - Not only
MKAnnotationViews
, also titles rendered belowMKMArkerAnnotationViews
need space. The rendering of those titles is influenced bymarkerView.titleVisibility
. IfmarkerView.titleVisibility
is set to.visible
(instead of the default.adaptive
), then this title is stronger than aMarkerAnnotationView
that is rendered later, even if the laterMarkerAnnotationView
has adisplayPriority = .required
. TheMarkerAnnotationView
nearer to the bottom is not rendered. - This even happens if the
MarkerAnnotationView
nearer to the top has a lowdisplayPriority
. So aMarkerAnnotationView
with lowdisplayPriority
and.titleVisibility = .visible
can make aMarkerAnnotationView
nearer to the bottom withdisplayPriority = .required
disappear.
那你应该怎么做:
- 对于所有相关注释,请确保
annotationView.displayPriority = .required
.(这是必要的,但还不够) - 不要不设置
annotationView.titleVisibility = .visible
,否则标题将使在底部附近呈现的注解视图消失.而是设置annotationView.titleVisibility = .adaptive
- make sure
annotationView.displayPriority = .required
for all relevant annotations. (This is necessary but not sufficient) - Do not set
annotationView.titleVisibility = .visible
or the title will make annotationViews disappear that are rendered nearer the bottom. Instead setannotationView.titleVisibility = .adaptive
我认为您无法完全控制可见性.但是 annotationView.titleVisibility = .visible
的问题令我感到惊讶,您应该避免使用它,因为它会隐藏注释.
I don't think you can control the visibility completely. But the problem with annotationView.titleVisibility = .visible
was surprising for me and you should avoid it because it will hide annotations.
如果使用群集,群集的行为类似于(也就是)注释,其行为完全如上所述.
Clusters behave like (and are) annotations that behave exactly as described above, if clusters are used.
这篇关于MKMapView防止注释分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!