【发布时间】:2022-01-21 21:41:39
【问题描述】:
我正在更新使用 Mapbox v6 SDK 的现有 iOS 应用。该应用程序使用点要素数组定义形状源,并指定应根据特定半径对点进行聚类。这是执行此操作的代码:
let source = MGLShapeSource(identifier: sourceIdentifier, features: features, options: [.clustered: true, .clusterRadius: 50.0, .maximumZoomLevelForClustering: 14.0])
我还定义了一个圆形样式图层以在地图上显示集群:
let circularClusterLayer = MGLCircleStyleLayer(identifier: "clusteredMarkers", source: source)
circularClusterLayer.predicate = NSPredicate(format: "cluster == YES")
这按预期工作,但现在我需要根据位于集群中的所有特征的属性来定义圆圈颜色。具体来说,我的每个功能在其属性字典中都有一个布尔属性,称为.needsHelp。我正在尝试根据此逻辑定义群集标记的圆圈颜色:
if any of the features in the cluster have .needsHelp == true {
// set the circle color = UIColor.red
circularClusterLayer.circleColor = NSExpression(forConstantValue: UIColor.red)
} else {
// set the circle color = UIColor.gray
circularClusterLayer.circleColor = NSExpression(forConstantValue: UIColor.gray)
}
我很确定我应该能够构建一个表达式,如果集群中的任何特征具有.needsHelp == true,则返回 true,否则返回 false。我相信这个表达式会被分配给源的 .clusterProperties 选项,如下所示:
let expression1 = NSExpression(mglJSONObject: ["any", ["==", ["boolean", ["get", "isInEmergency"]], true]])
let expression2 = NSExpression(forConstantValue: UIColor.red)
let clusterPropertyDictionary: NSDictionary = ["clusterContainsFeatureThatNeedsHelp" : [expression1, expression2]]
let selectedContactsSource = MGLShapeSource(identifier: sourceIdentifier, features: features, options: [.clustered: true, .clusterRadius: 50.0, .maximumZoomLevelForClustering: 14.0, .clusterProperties: clusterPropertyDictionary])
我一直在查看Mapbox expressions guide,我认为我应该使用“任何”决策表达式来返回一个布尔值,指示是否有任何功能具有.needsHelp == true。但到目前为止,我一直无法将类似 LISP 的表达式语法翻译成有效的 NSExpression。
我假设除了上面的表达式,我还需要另一个表达式来检查“clusterContainsFeatureThatNeedsHelp”的值,然后相应地设置圆圈颜色。我最好的猜测是它看起来像这样:
// TODO: check value of clusterContainsFeatureThatNeedsHelp to determine circle color
circularClusterLayer.circleColor = NSExpression(forConditional: NSPredicate(format: "%K == %@", "clusterContainsFeatureThatNeedsHelp", NSNumber(value: true)), trueExpression: NSExpression(forConstantValue: UIColor.red), falseExpression: NSExpression(forConstantValue: UIColor.gray))
不幸的是,我没有成功构建这些 NSExpressions。有没有人做过这样的事情?如果是这样,我将不胜感激任何提示或建议!
【问题讨论】:
标签: ios swift mapbox nsexpression mapbox-expressions