消除 MKMapView 中的引脚重叠
我正在使用基于 MKMapView 的应用程序。我需要澄清是否可以消除 MKMapView 中的引脚 OverLap?因为有些地方有大量的图钉显示。我很难识别引脚。
I am working with MKMapView Based application. I need a clarification whether it is possible to eliminate the pin OverLap in the MKMapView? Because at some places there are large number of pins displaying. It is difficult to me to identify the pins.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您有 Apple 开发者帐户,我强烈建议您获取 2011 WWDC 大会的第 111 期视频会议,标题为“使用 MapKit 可视化地理信息”。其中一个部分专门介绍了如何对大型数据集中的内容进行聚类,以便您可以根据不同缩放级别的密度对图钉进行分组或取消分组。
他们的示例非常简单,但问题的核心是您想要用单个引脚替换一组重叠的引脚,并且当您放大时,单个引脚将分裂回各个引脚。
您决定如何以及何时对事物进行分组可能会有很大差异。苹果的解决方案只是将地图细分为一个网格,任何具有超过 1 个图钉的方框都会形成一个组。您还可以采用算法方法,例如使用 kMeansCluster 算法,该算法非常简单,您可以通过该算法提供所有注释,并从另一端获得逻辑组织的一组组。
从那里开始,需要跟踪所有单独的引脚以及在放大和缩小时它们如何分组。您将仅显示每个组或剩余的任何单个引脚的单个注释。还可以在地图放大和缩小时为过渡设置动画,以便您可以在视觉上强化正在发生的事情。
我自己的技术与 Apple 的方法密切相关,因此我无法在这里发布,因此我希望您可以访问上面的视频,其中涵盖了几乎所有这些要点。
If you have an Apple Developer Account, I would strongly recommend getting the Session 111 video from the 2011 WWDC Conference Sessions, entitled "Visualizing Information Geographically with MapKit". One of the segments specifically covers how to cluster content from large data sets to allow you to group or ungroup pins based on density at various zoom levels.
Their example is elegantly simple, but at the heart of the problem you want to replace a group of overlapping pins with a single pin and as you zoom in the single pin will split back into the individual pins.
How and when you decide to group things can be varied considerably. Apple's solution simply subdivides the map into a grid and any box that has more than 1 pin results in a group. You could also take an algorithmic approach such as using a kMeansCluster algorithm which is incredibly simple and you could feed all of your annotations through the algorithm and get an array of groups out the other side logically organized.
From there it's a matter of keeping track of all the individual pins and how they are grouped as you zoom in and out. You will only display a single annotation for each group or any individual pins that are left over. It's also possible to animate the transitions as the map zooms in and out so you can visually reinforce what is happening.
My own technique is too closely related to Apple's approach for me to post here so I'm hoping you can access the above video which covers almost all of these points.
为此,您必须在地图中实现聚类概念。通过使用 Apple 演示代码,可以轻松在我们的代码中实现聚类概念。 参考链接
我们可以简单地使用以下代码进行聚类
实现聚类的步骤
Step1: 重要的是对于聚类,我们使用两个mapview(allAnnotationsMapView, ),一个用于参考(allAnnotationsMapView)。
在viewDidLoad中
Step2:将所有注释添加到_allAnnotationsMapView中,下面的_photos是注释数组。
Step3: 添加以下聚类方法,其中 PhotoAnnotation 是自定义注释。
MapViewDelegate方法
聚类处理方法
通过以上步骤我们就可以在mapview上实现聚类,不需要使用任何第三方代码或框架。请在此处查看 Apple 示例代码。如果您对此有任何疑问,请告诉我。
For this you have to implement clustering concept to your map.By using Apple demo code it's easy to implement clustering concept in our code. Reference link
Simply we can use following code for the Clustering
Steps to implement clustering
Step1 : The important thing is for clustering we use two mapviews(allAnnotationsMapView, ), One is for reference(allAnnotationsMapView).
In viewDidLoad
Step2 : Add all annotations to the _allAnnotationsMapView, In below _photos are the annotations array.
Step3 : Add below methods for clustering, in this PhotoAnnotation is the custom annotation.
MapViewDelegate methods
clustering Handling methods
By following above steps we can achieve clustering on mapview, it is not necessary to use any third party code or framework. Please check the Apple sample code here. Please let me know if you have any doubts on this.
实现您自己的注释集群框架非常容易。下面是一个基本示例,您可以在此处进行参考。
It's quite easy to implement your own annotation clustering framework. Here's an example of a basic one that you can refer here.
如果您的图钉重叠,那么该位置的缩放级别一定很高。
您可以考虑删除该缩放级别中的一些注释,直到没有注释重叠,并且在放大时可以添加注释,以便注释之间有足够的空间。
If your pins are overlapping then it must be that your zoom level is high for that place.
You can think of removing some annotations in that zoom level until you dont have annotation overlaps and while zooming in you can add the annotations so that there are enough space between the annotations.