Sort annotations in Mapbox

I'm currently looking for sorting all annotations using mapbox.

I know 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 problem here is that I lost information about where I should name this process.

I use mapView:layerForAnnotation: to return shapes that should be drawn, but if I'm not mistaken, this is a callback that is not really called from the code.

Thank you for your help.

EDIT:

Thanks to jszumski, I understood the implementation. For those who need it in the future, this 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; } 
+7
sorting ios objective-c mapbox
source share
1 answer

Assuming your comparator is correctly implemented, it should just work. I don’t think you need to remove and re-add all annotations unless you explicitly change the comparator logic every time.

From the documentation :

The block will be called repeatedly during map change events to ensure that the annotation layers are saved in the desired order.

+2
source share

All Articles