对Mapbox注释进行排序

3wi*_*wic 7 sorting objective-c ios mapbox

我目前正在寻找使用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];
Run Code Online (Sandbox Code Playgroud)

这里的问题是我迷失了我应该称之为这个过程的地方.

我实际上是mapView:layerForAnnotation:用来返回应该绘制的形状,但如果我没有错,那么它是一个回调,所以不是从代码中调用的.

谢谢你的帮助.

编辑:

感谢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;
}
Run Code Online (Sandbox Code Playgroud)

jsz*_*ski 2

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

文档中:

在地图更改事件期间将重复调用该块,以确保注释图层保持按所需顺序排序。