首页 > 解决方案 > Mapbox 表达式检查 MGLStyleLayer 的属性并与字符串进行比较

问题描述

在该mapView:didFinishLoadingStyle:方法中,我希望能够检查 a 是否MGLSymbolStyleLayer使用特定的class,类似于我们如何filter在 JSON 样式文件中使用将样式仅应用于某个类:

在此处输入图像描述

但是,就我而言,我想检查地图上是否存在一个类,如果它是字符串数组的一部分,则将其隐藏:

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

这给了我错误:

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

关于如何编写NSExpression能够将class属性与另一个字符串进行比较的任何帮助?

标签: iosmapboxmapbox-glmapbox-ios

解决方案


我在这上面浪费了很多时间,最后它比我想象的要容易:

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

推荐阅读