Created
October 15, 2020 20:08
-
-
Save avi-c/53f25c622a46be6fab20705fd91c9e3b to your computer and use it in GitHub Desktop.
Swift code showing how to set the opacity of a MGLSymbolStyleLayer based on a feature property
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private func applyOpacity(style: MGLStyle) { | |
// NSExpression *iconOpacityStyle = | |
// [NSExpression expressionWithFormat:@"MGL_MATCH(%@, %@, %f, %f)", selectedAttributeName, selectedAttributeValue, | |
// selectedIconOpacity, deselectedIconOpacity]; | |
// works for the Nav V5 style to fade away all the road shields that aren't for I-280 | |
if let symbolLayer = style.layer(withIdentifier: "road-number-shield-navigation") as? MGLSymbolStyleLayer { | |
let selectedAttributeName = "ref" | |
let selectedAttributeValue = "280" | |
let selectedIconOpacity = NSExpression(forConstantValue: NSNumber(1.0)) | |
let deselectedIconOpacity = NSExpression(forConstantValue: NSNumber(0.1)) | |
let iconOpacityExpression = NSExpression(format: "MGL_MATCH(\(selectedAttributeName), %@, %@, %@)", selectedAttributeValue, selectedIconOpacity, deselectedIconOpacity) | |
symbolLayer.iconOpacity = iconOpacityExpression | |
} | |
// my version for Snap with a placeholder layer identifier and a set value for the place_id field from the attributes they provided | |
if let symbolLayer = style.layer(withIdentifier: "my-snap-poi-layer") as? MGLSymbolStyleLayer { | |
let selectedAttributeName = "place_id" | |
let selectedAttributeValue = "3fc061dc-9993-11e8-bcbe-fffaaefe62e0" | |
let selectedIconOpacity = NSExpression(forConstantValue: NSNumber(1.0)) | |
let deselectedIconOpacity = NSExpression(forConstantValue: NSNumber(0.1)) | |
let iconOpacityExpression = NSExpression(format: "MGL_MATCH(\(selectedAttributeName), %@, %@, %@)", selectedAttributeValue, selectedIconOpacity, deselectedIconOpacity) | |
symbolLayer.iconOpacity = iconOpacityExpression | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment