如何重新排序的MKMapView注释阵列 [英] How to reorder MKMapView annotations array

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

问题描述

我想重新排序,以创建通过所有注释快速循环(按日期排序)使用一个简单的迭代器下一个/ $ P $光伏按钮显示在地图上标注的数组。

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.

据我看到作为一个存储注解阵列的 [注解世界地图] 的不可改变,所以我不能重新排序。我尝试了以下创建注释数组的临时副本,其排序按日期和重新连接它。

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.

世界地图的是我的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?

推荐答案

由于在<一个答案href=\"http://stackoverflow.com/questions/9539802/mkmapview-annotations-changing-losing-order?lq=1\">linked问题提到,没有保证地图视图的注释属性将preserve任何顺序。

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

此外,因为注释属性的包括的的 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.

不要依赖地图视图的注释数组中的注释的的为了的上。

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.

请该地图视图的注释数组可以包含 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.

结果
但是,请注意,code对数组进行排序:


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天全站免登陆