MKAnnotation的独特性
我有一个 MKMapView,它查询服务器并在中心更改时显示 MKAnnotations 数组,但自从我读取它们并添加它们以来,我遇到了重复 MKAnnotations 的问题。
我正在考虑用 for 查看每个注释,如果它已经存在,则不执行任何操作,但我认为这不是解决此问题的最佳解决方案。
有什么想法吗?
编辑:我目前正在删除所有这些并再次添加它们,这会为用户产生一个轻弹,这是我不希望发生的事情
I have a MKMapView that queries a server and displays an array of MKAnnotations when the center is changed but i'm experiencing the issue of duplicate MKAnnotations since I read them and add them.
I'm thinking on looking at each Annotation with a for and if it's already there do nothing, but I think is not the best solution to solve this.
Any ideas?
Edit: I'm currently removing all of them and adding them again, which produces a flick for the user, thing i don't want to happen
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在注释中,您可以使用一个属性来存储唯一标识符(例如主键)。当您获得刷新的项目时,您可以搜索现有项目并添加(如果不存在)。
或者
您可以拥有一个本地可变数组并存储主键。搜索该数组并添加到数组并映射(如果键不存在)。
在任何一种情况下,如果用户继续平移,您最终都会向地图视图添加许多注释。
确保使用以便可以重用视图
In your annotation you can have a property to store a unique identifier like a primary key. When you get a the refreshed items you can search for the existing ones and add if not present.
OR
You can have a local mutable array and store the primary keys. Search that array and add to the array and map if the keys is not present.
On either case if the user keeps panning you will end up adding many annotation to the mapview.
Make sure you use so that you can reuse the views
基本选项
根据您的情况,最简单的方法可能是简单地删除所有现有选项,然后添加所有查询的选项。
您是否遇到了一个特殊的问题,即这对您来说效果不佳?
更高级的选项
也许另一个更有趣的选项是像Google那样将地图分割成网格(缩放和网格x,y)来自服务器的查询应该专门针对网格空间,或一组网格空间 - 这样您就不会首先重新请求重复的信息。它还可以让您更轻松地确定哪些内容已加载,哪些内容尚未加载。您可以在后台预先请求周围的网格空间,并且仅在用户滚动出“已加载”区域时才加载新的网格空间。
Basic Options
Depending on your situation, it might be easiest to simply remove all of the existing ones, and add all of your queried ones.
Are you having a particular problem where this isn't working well for you?
More Advanced Option
Perhaps another, more interesting option, would be to split your map into grids the way Google does (zoom and grid x,y) The queries from the server should be specifically for a grid space, or a set of grid spaces - this way you're not re-requesting duplicate information in the first place. It also would make it easier for you to determine what is and what isn't already loaded. You could pre-request surrounding grid spaces in the background, and only load new grid spaces when the user scrolls out of the "already loaded" area.