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.
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;
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With