Search code examples
iosmapboxmapbox-glmapbox-ios

Mapbox Expression to check property of MGLStyleLayer and compare with string


In the mapView:didFinishLoadingStyle: method, I would like to be able to check to see if a MGLSymbolStyleLayer uses a specific class, similar to how we can use filter in a JSON style file to apply a style to only a certain class:

enter image description here

However, in my case, I want to check and see if a class exists on the map, and then hide it if it is part of an array of strings:

NSArray *poiTypesExcluded = @[@"airport"];
for (NSString *poiType in poiTypesExcluded) {
        
    layer.visible = [NSExpression expressionWithFormat:@"class != %@", poiType];
        
}

This is giving me the error:

Unable to parse the format string "class != %@ == 1".

Any help on how to write the NSExpression to be able to compare a class property to another string?


Solution

  • I wasted a lot of time on this, and in the end it was easier than I thought:

    NSArray *poiTypesExcluded = @[@"airport", @"grocery", @"bank"];
    NSMutableArray *predicates = [[NSMutableArray alloc] init];
    for (NSString *poiType in poiTypesExcluded) {
        
        [predicates addObject:[NSPredicate predicateWithFormat:@"class != %@", poiType]];
        
    }
    
    symbolLayer.predicate = [NSCompoundPredicate andPredicateWithSubpredicates:predicates];