ios - 排序 Mapbox 注释

标签 ios objective-c sorting 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];

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

我实际上是在使用 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;
}

最佳答案

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

来自 the documentation :

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

关于ios - 排序 Mapbox 注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29766837/

相关文章:

ios - 如何在 iOS 上将 flac 转换为 wav?

python - 如何使用 numpy.argsort() 作为二维以上的索引?

python - 对工作日文本进行排序

ios - 有效负载安装顺序是否未定义?

ios - `onNotificationOpened` 当应用程序被杀死或从 iOS 内存中删除时不会被调用

ios - Debug模式下和试飞部署后的不同应用显示分辨率

ios - 当没有 ScrollView 正在减速时,UIScrollView isDragging 返回 YES

ios - 取决于互联网连接显示 ViewController iOS

Objective-C 文件在 macOS Catalina 上编译缓慢

javascript - 按属性对对象 javascript 数组进行排序