我可以在 map 上绘制折线路线,但我无法从 map View 中清除这条路线。我的代码是:
func drawRoute() {
var routeCoordinates = route.coordinates!
let polyline = MGLPolylineFeature(coordinates: &routeCoordinates, count: route.coordinateCount)
let source = MGLShapeSource(identifier: "polyline", features: [polyline], options: nil)
style.addSource(source)
let layer = MGLLineStyleLayer(identifier: "polyline", source: source)
layer.lineJoin = MGLStyleValue(rawValue: NSValue(mglLineJoin: .round))
layer.lineCap = MGLStyleValue(rawValue: NSValue(mglLineCap: .round))
layer.lineColor = MGLStyleValue(rawValue: UIColor(red: 31 / 255, green: 31 / 255, blue: 31 / 255, alpha: 1))
layer.lineWidth = MGLStyleValue(interpolationMode: .exponential, cameraStops: [14: MGLStyleValue<NSNumber>(rawValue: 2), 18: MGLStyleValue<NSNumber>(rawValue: 20)], options: [.defaultValue : MGLConstantStyleValue<NSNumber>(rawValue: 1.5)])
let casingLayer = MGLLineStyleLayer(identifier: "polyline-case", source: source)
casingLayer.lineJoin = layer.lineJoin
casingLayer.lineCap = layer.lineCap
casingLayer.lineGapWidth = layer.lineWidth
casingLayer.lineColor = MGLStyleValue(rawValue: UIColor(red: 0 / 255, green: 0 / 255, blue: 0 / 255, alpha: 1))
casingLayer.lineWidth = MGLStyleValue(interpolationMode: .exponential, cameraStops: [14: MGLStyleValue(rawValue: 1), 18: MGLStyleValue(rawValue: 4)], options: [.defaultValue : MGLConstantStyleValue<NSNumber>(rawValue: 1.5)])
let dashedLayer = MGLLineStyleLayer(identifier: "polyline-dash", source: source)
dashedLayer.lineJoin = layer.lineJoin
dashedLayer.lineCap = layer.lineCap
dashedLayer.lineColor = MGLStyleValue(rawValue: .white)
dashedLayer.lineOpacity = MGLStyleValue(rawValue: 1.0)
dashedLayer.lineWidth = layer.lineWidth
dashedLayer.lineDashPattern = MGLStyleValue(rawValue: [0, 1.5])
style.addLayer(layer)
style.addLayer(dashedLayer)
style.insertLayer(casingLayer, below: layer)
}
我想清除这条折线并用不同的坐标重新绘制。我尝试了 removeAnnotation 方法,但没有帮助。谢谢。
最佳答案
我找到了解决方案。我们可以使用 self.mapView.style?.source(withIdentifier: "polyline")
这样的标识符访问源,然后将其删除。
关于ios - Mapbox 清除路线折线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49810714/