Skip to content

Commit 3ae3cf3

Browse files
committed
Replace JSON with Expressions in route duration annotation styling
1 parent 69350fd commit 3ae3cf3

File tree

1 file changed

+22
-56
lines changed

1 file changed

+22
-56
lines changed

Sources/MapboxNavigation/NavigationMapView.swift

+22-56
Original file line numberDiff line numberDiff line change
@@ -1237,66 +1237,32 @@ open class NavigationMapView: UIView {
12371237
shapeLayer.textJustify = .constant(TextJustify.left)
12381238
shapeLayer.symbolZOrder = .constant(SymbolZOrder.auto)
12391239
shapeLayer.textFont = .constant(self.routeDurationAnnotationFontNames)
1240+
1241+
shapeLayer.symbolSortKey = .expression(Exp(.get) {
1242+
"sortOrder"
1243+
})
12401244

1241-
try style.addLayer(shapeLayer)
1242-
1243-
let symbolSortKeyString =
1244-
"""
1245-
["get", "sortOrder"]
1246-
"""
1247-
1248-
if let expressionData = symbolSortKeyString.data(using: .utf8),
1249-
let expJSONObject = try? JSONSerialization.jsonObject(with: expressionData, options: []) {
1250-
let property = "symbol-sort-key"
1251-
try style.setLayerProperty(for: routeDurationAnnotationsLayerIdentifier,
1252-
property: property,
1253-
value: expJSONObject)
1245+
let anchorExpression = Exp(.match) {
1246+
Exp(.get) { "tailPosition" }
1247+
0
1248+
"bottom-left"
1249+
1
1250+
"bottom-right"
1251+
"center"
12541252
}
1255-
1256-
let expressionString =
1257-
"""
1258-
[
1259-
"match",
1260-
["get", "tailPosition"],
1261-
[0],
1262-
"bottom-left",
1263-
[1],
1264-
"bottom-right",
1265-
"center"
1266-
]
1267-
"""
1268-
1269-
if let expressionData = expressionString.data(using: .utf8),
1270-
let expJSONObject = try? JSONSerialization.jsonObject(with: expressionData, options: []) {
1271-
try style.setLayerProperty(for: routeDurationAnnotationsLayerIdentifier,
1272-
property: "icon-anchor",
1273-
value: expJSONObject)
1274-
try style.setLayerProperty(for: routeDurationAnnotationsLayerIdentifier,
1275-
property: "text-anchor",
1276-
value: expJSONObject)
1253+
shapeLayer.iconAnchor = .expression(anchorExpression)
1254+
shapeLayer.textAnchor = .expression(anchorExpression)
1255+
1256+
let offsetExpression = Exp(.match) {
1257+
Exp(.get) { "tailPosition" }
1258+
0
1259+
Exp(.literal) { [0.5, -1.0] }
1260+
Exp(.literal) { [-0.5, -1.0] }
12771261
}
1278-
1279-
let offsetExpressionString =
1280-
"""
1281-
[
1282-
"match",
1283-
["get", "tailPosition"],
1284-
[0],
1285-
["literal", [0.5, -1]],
1286-
["literal", [-0.5, -1]]
1287-
]
1288-
"""
1262+
shapeLayer.iconOffset = .expression(offsetExpression)
1263+
shapeLayer.textOffset = .expression(offsetExpression)
12891264

1290-
if let expressionData = offsetExpressionString.data(using: .utf8),
1291-
let expJSONObject = try? JSONSerialization.jsonObject(with: expressionData, options: []) {
1292-
try style.setLayerProperty(for: routeDurationAnnotationsLayerIdentifier,
1293-
property: "icon-offset",
1294-
value: expJSONObject)
1295-
1296-
try style.setLayerProperty(for: routeDurationAnnotationsLayerIdentifier,
1297-
property: "text-offset",
1298-
value: expJSONObject)
1299-
}
1265+
try style.addLayer(shapeLayer)
13001266
}
13011267

13021268
/**

0 commit comments

Comments
 (0)