如何重新排序 MKMapView 注释数组 [英] How to reorder MKMapView annotations array

查看:26
本文介绍了如何重新排序 MKMapView 注释数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对地图上显示的注释数组重新排序,以便创建下一个/上一个按钮,以便使用简单的迭代器快速循环浏览所有注释(按日期排序).

I'd like to reorder the array of annotations shown on a map in order to create a next/prev button for quickly cycling through all annotations (sorted by date) using a simple iterator.

据我所知,用作存储的 annotations 数组 [worldmap annotations] 不是可变的,因此我无法对其重新排序.我尝试了以下方法来创建 annotations 数组的临时副本,按日期对其进行排序并重新附加它.

As far as I see the annotations array used as a store [worldmap annotations] is not mutable and therefore I cannot reorder it. I tried the following to create a temporary copy of the annotations array, sort it by date and re-attach it.

(worldmap 是我的 MKMapView 对象)

(worldmap is my MKMapView object)

//COPY
 NSMutableArray *annotationSort = [[NSMutableArray alloc]initWithArray:[worldmap annotations]];

//SORT
[annotationSort sortedArrayUsingComparator:^NSComparisonResult(EventPin* obj1, EventPin* obj2) {
     return [obj1.eventItemObject.eventDateBegin compare: obj2.eventItemObject.eventDateBegin];
 }];

//ADDED SORTED ARRAY
[worldmap removeAnnotations:[worldmap annotations]];
[worldmap addAnnotations:annotationSort];

这似乎不起作用.知道如何对 MKMapKit 注释数组重新排序吗?

This doesn't seem to work. Any idea how can I reorder the MKMapKit annotations array?

推荐答案

作为 链接问题 提到,不能保证地图视图的 annotations 属性将保留任何顺序.

As the answer in the linked question mentions, there is no guarantee that the map view's annotations property will preserve any order.

此外,由于 annotations 属性 includes userLocation 如果您打开了 showsUserLocation(但您自己没有明确调用 addAnnotation ),注释顺序将不是您所期望的.

In addition, since the annotations property includes the userLocation if you have showsUserLocation turned on (but which you don't yourself explicitly call addAnnotation for), the annotation order will not be what you may expect.

不要依赖地图视图的 annotations 数组中注释的 顺序.

Don't rely on the order of the annotations in the map view's annotations array.

相反,保留您自己对注释的引用数组,并按您想要的任何方式对其进行排序(如您的 annotationSort 数组).
但是将它们从地图中删除并重新添加回去是没有意义的.

Instead, keep your own array of references to the annotations and sort them any way you want (like your annotationSort array).
But there's no point in removing them from the map and adding them back.

请记住,地图视图的 annotations 数组可能包含 MKUserLocation 注释,因此在构造数组时,请在包含注释或访问自定义属性之前检查每个注释的类型.

Keep in mind that the map view's annotations array may contain the MKUserLocation annotation so when constructing your array, check the type of each annotation before including it or accessing custom properties.


但是,请注意对数组进行排序的代码:


However, note that the code to sort the array:

[annotationSort sortedArrayUsingComparator:...

本身存在缺陷,因为 sortedArrayUsingComparator 返回一个 NSArray.
它不会就地对数组进行排序.

is flawed itself because sortedArrayUsingComparator returns an NSArray.
It does not sort the array in-place.

相反,要对 NSMutableArray 进行排序,请调用其 sortUsingComparator 方法.

Instead, to sort an NSMutableArray, call its sortUsingComparator method.


使用此排序数组,您的应用可以按所需顺序访问或选择注释.


Using this sorted array, your app can access or select the annotations in the order desired.

这篇关于如何重新排序 MKMapView 注释数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆