ios - Mapbox 清除路线折线

标签 ios swift mapbox

我可以在 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/

相关文章:

iPhone 4 对比。 4s wifi数据上传速度差异

iphone - iOS 在蓝牙 session 中可以连接到 gamecenter 比赛的最大用户数是多少?

ios - 在接收推送时获取位置

ios - Swift 解析查询结果未出现在 TableView 中

javascript - mapbox-gl-js:针对给定间距将可见区域和方位调整到给定线

ios - 自定义 iPhone 框架的 Mach-O 类型应该是什么?

swift - Xcode 9 项目中的操作被破坏

ios - 添加 TVOS 目标并更改 Podfile 结构后,Xcode/Cocoapods 项目搞砸了

javascript - Mapbox simplestyle 会阻止 popupContent?使用 pointToLayer 和 onEachFeature

css - 在包含的父 div 中定位一个 map div