排序 Mapbox 注释

Sort Mapbox annotations

我目前正在寻找使用 mapbox 对所有注释进行排序。

我知道我应该使用以下流程:

//implementing this method and do the sort here   
(NSComparator)annotationSortingComparatorForMapView:(RMMapView *)mapView

// remove current annotation and use the sorted array to redraw them
[mapView removeAnnotations:[mapView annotations]];
[mapView addAnnotations:annotationSorted];

这里的问题是我不知道应该在哪里调用这个过程。

我实际上是在使用 mapView:layerForAnnotation: 来 return 应该绘制的形状,但如果我没记错的话,它是一个回调,所以并没有真正从代码中调用。

感谢您的帮助。

编辑:

感谢 jszumski,我找到了一个实现。对于那些将来需要它的人来说:

- (NSComparator)annotationSortingComparatorForMapView:(RMMapView *)RmapView
{
    NSComparator sort =^(RMAnnotation *annotation1, RMAnnotation *annotation2)
    {
        // Sort user location annotations above all.
        //
        if (   annotation1.isUserLocationAnnotation && ! annotation2.isUserLocationAnnotation)
            return NSOrderedDescending;

        if ( ! annotation1.isUserLocationAnnotation &&   annotation2.isUserLocationAnnotation)
            return NSOrderedAscending;

        // Amongst user location annotations, sort properly.
        //
        if (annotation1.isUserLocationAnnotation && annotation2.isUserLocationAnnotation)
        {
            // User dot on top.
            //
            if ([annotation1 isKindOfClass:[RMUserLocation class]])
                return NSOrderedDescending;

            if ([annotation2 isKindOfClass:[RMUserLocation class]])
                return NSOrderedAscending;

            // Halo above accuracy circle.
            //
            if ([annotation1.annotationType isEqualToString:kRMTrackingHaloAnnotationTypeName])
                return NSOrderedDescending;

            if ([annotation2.annotationType isEqualToString:kRMTrackingHaloAnnotationTypeName])
                return NSOrderedAscending;
        }


        return NSOrderedSame;
    };
    return sort;
}

假设您的比较器已正确实现,它应该可以正常工作。我认为您不需要删除并重新添加所有注释,除非您每次都明确更改比较器的逻辑。

来自the documentation

The block will be called repeatedly during map change events to ensure annotation layers stay sorted in the desired order.