Search code examples
objective-ccocoa-touchiosmkmapviewmkannotation

How to select annotations of specific class in visible map rect


I am trying to implement the method below which basically populates my array with the annotations visible in the current map rect.

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
    self.LocationSet = [self.mapView annotationsInMapRect:self.mapView.visibleMapRect];

    self.LocationArray = [self.nearbyCarParksSet allObjects];

}

What is the right way to select only a certain class of annotations which I want to include in the set/array? For example, in the current map view I might have annotations belonging to a "Restaurant" class, a "carpark" class, a "petrol station" class, etc., and I only want to store annotations belonging to the "Restaurant" class. What is the best approach?


Solution

  • Assuming that locationSet has all the annotation derived from the first call in the method above. You can get all annotations of type using,

    restaurantSet = [locationSet objectsPassingTest:^(id obj, BOOL *stop){
        return [obj isMemberOfClass:[YourAnnotationSubclass class]];
    }];