From 0f688b0bda3f10f60e2c5442296a2b7fb345688a Mon Sep 17 00:00:00 2001 From: bsudekum Date: Mon, 27 Mar 2017 10:08:47 -0700 Subject: [PATCH] Update --- MapboxNavigationUI/MGLMapView.swift | 96 +++++++++++++++-------------- 1 file changed, 50 insertions(+), 46 deletions(-) diff --git a/MapboxNavigationUI/MGLMapView.swift b/MapboxNavigationUI/MGLMapView.swift index 843ba319cb5..cc7cfdb5479 100644 --- a/MapboxNavigationUI/MGLMapView.swift +++ b/MapboxNavigationUI/MGLMapView.swift @@ -34,31 +34,35 @@ extension MGLMapView { } let polyline = MGLPolylineFeature(coordinates: &coordinates, count: route.coordinateCount) - let geoJSONSource = MGLShapeSource(identifier: sourceIdentifier, shape: polyline, options: nil) - let line = MGLLineStyleLayer(identifier: routeLayerIdentifier, source: geoJSONSource) - let lineCasing = MGLLineStyleLayer(identifier: routeLayerCasingIdentifier, source: geoJSONSource) - line.lineColor = MGLStyleValue(rawValue: NavigationUI.shared.tintStrokeColor.withAlphaComponent(0.6)) - line.lineWidth = MGLStyleValue(rawValue: 5) - lineCasing.lineColor = MGLStyleValue(rawValue: NavigationUI.shared.tintStrokeColor) - lineCasing.lineWidth = MGLStyleValue(rawValue: 9) - - let cap = NSValue(mglLineCap: .round) - let join = NSValue(mglLineJoin: .round) - - line.lineCap = MGLStyleValue(rawValue: cap) - line.lineJoin = MGLStyleValue(rawValue: join) - lineCasing.lineCap = MGLStyleValue(rawValue: cap) - lineCasing.lineJoin = MGLStyleValue(rawValue: join) - - style.addSource(geoJSONSource) - - for layer in style.layers.reversed() { - if let layer = layer as? MGLStyleLayer, !(layer is MGLSymbolStyleLayer) && - layer.identifier != arrowLayerIdentifier && layer.identifier != arrowSourceIdentifier { - style.insertLayer(line, above: layer) - style.insertLayer(lineCasing, below: line) - return + if let source = style.source(withIdentifier: sourceIdentifier) as? MGLShapeSource { + source.shape = polyline + } else { + let geoJSONSource = MGLShapeSource(identifier: sourceIdentifier, shape: polyline, options: nil) + let line = MGLLineStyleLayer(identifier: routeLayerIdentifier, source: geoJSONSource) + let lineCasing = MGLLineStyleLayer(identifier: routeLayerCasingIdentifier, source: geoJSONSource) + + line.lineColor = MGLStyleValue(rawValue: NavigationUI.shared.tintStrokeColor.withAlphaComponent(0.6)) + line.lineWidth = MGLStyleValue(rawValue: 5) + lineCasing.lineColor = MGLStyleValue(rawValue: NavigationUI.shared.tintStrokeColor) + lineCasing.lineWidth = MGLStyleValue(rawValue: 9) + + let cap = NSValue(mglLineCap: .round) + let join = NSValue(mglLineJoin: .round) + + line.lineCap = MGLStyleValue(rawValue: cap) + line.lineJoin = MGLStyleValue(rawValue: join) + lineCasing.lineCap = MGLStyleValue(rawValue: cap) + lineCasing.lineJoin = MGLStyleValue(rawValue: join) + + style.addSource(geoJSONSource) + for layer in style.layers.reversed() { + if let layer = layer as? MGLStyleLayer, !(layer is MGLSymbolStyleLayer) && + layer.identifier != arrowLayerIdentifier && layer.identifier != arrowSourceIdentifier { + style.insertLayer(line, above: layer) + style.insertLayer(lineCasing, below: line) + return + } } } } @@ -132,39 +136,39 @@ extension MGLMapView { maneuverArrowPolylines.append(headStrokePolyline) - let arrowSource = MGLShapeSource(identifier: arrowSourceIdentifier, shape: MGLShapeCollection(shapes: maneuverArrowPolylines), options: nil) - let arrow = MGLLineStyleLayer(identifier: arrowLayerIdentifier, source: arrowSource) - - arrow.lineWidth = MGLStyleValue(rawValue: 6) - arrow.lineColor = MGLStyleValue(rawValue: .white) - - // Arrow stroke - let arrowSourceStroke = MGLShapeSource(identifier: arrowSourceStrokeIdentifier, shape: MGLShapeCollection(shapes: maneuverArrowStrokePolylines), options: nil) - let arrowStroke = MGLLineStyleLayer(identifier: arrowSourceIdentifier, source: arrowSourceStroke) + let arrowShape = MGLShapeCollection(shapes: maneuverArrowPolylines) + let arrowStrokeShape = MGLShapeCollection(shapes: maneuverArrowStrokePolylines) let cap = NSValue(mglLineCap: .round) let join = NSValue(mglLineJoin: .round) - arrowStroke.lineCap = MGLStyleValue(rawValue: cap) - arrowStroke.lineJoin = MGLStyleValue(rawValue: join) - arrow.lineCap = MGLStyleValue(rawValue: cap) - arrow.lineJoin = MGLStyleValue(rawValue: join) - - arrowStroke.lineWidth = MGLStyleValue(rawValue: 8) - arrowStroke.lineColor = MGLStyleValue(rawValue: NavigationUI.shared.tintColor) + let arrowSourceStroke = MGLShapeSource(identifier: arrowSourceStrokeIdentifier, shape: arrowStrokeShape, options: nil) + let arrowStroke = MGLLineStyleLayer(identifier: arrowSourceIdentifier, source: arrowSourceStroke) + let arrowSource = MGLShapeSource(identifier: arrowSourceIdentifier, shape: arrowShape, options: nil) + let arrow = MGLLineStyleLayer(identifier: arrowLayerIdentifier, source: arrowSource) - if let source = style.source(withIdentifier: arrowSourceIdentifier) { - let s = source as! MGLShapeSource - s.shape = MGLShapeCollection(shapes: maneuverArrowPolylines) + if let source = style.source(withIdentifier: arrowSourceIdentifier) as? MGLShapeSource { + source.shape = arrowShape } else { + + arrow.lineCap = MGLStyleValue(rawValue: cap) + arrow.lineJoin = MGLStyleValue(rawValue: join) + arrow.lineWidth = MGLStyleValue(rawValue: 6) + arrow.lineColor = MGLStyleValue(rawValue: .white) + style.addSource(arrowSource) style.addLayer(arrow) } - if let source = style.source(withIdentifier: arrowSourceStrokeIdentifier) { - let s = source as! MGLShapeSource - s.shape = MGLShapeCollection(shapes: maneuverArrowStrokePolylines) + if let source = style.source(withIdentifier: arrowSourceStrokeIdentifier) as? MGLShapeSource { + source.shape = arrowStrokeShape } else { + + arrowStroke.lineCap = MGLStyleValue(rawValue: cap) + arrowStroke.lineJoin = MGLStyleValue(rawValue: join) + arrowStroke.lineWidth = MGLStyleValue(rawValue: 8) + arrowStroke.lineColor = MGLStyleValue(rawValue: NavigationUI.shared.tintColor) + style.addSource(arrowSourceStroke) style.insertLayer(arrowStroke, below: arrow) }