How to reorder MKMapView annotations array

前端 未结 1 986
感情败类
感情败类 2020-12-12 03:12

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 simpl

相关标签:
1条回答
  • 2020-12-12 04:06

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

    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.

    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.

    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:...
    

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

    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.

    0 讨论(0)
提交回复
热议问题