Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort Mapbox annotations

I'm currently looking to sort all annotation using mapbox.

I know that I should use the following process :

//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];

The issue here is that I'm lost about where I should call this process.

I'm actually using mapView:layerForAnnotation: to return the shapes that should be draw but if I'm not wrong, it's a callback so not really invoked from the code.

Thanks for your help.

EDIT:

Thanks to jszumski I figured out a implementation. For those who need it in the futur there it is :

- (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;
}
like image 713
3wic Avatar asked Apr 21 '15 08:04

3wic


1 Answers

Assuming your comparator is correctly implemented, it should just work. I don't believe you need to remove and re-add all the annotations unless you are explicitly changing the logic of the comparator each time.

From the documentation:

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

like image 150
jszumski Avatar answered Nov 16 '22 17:11

jszumski