Mapbox清除路线折线

时间:2018-04-13 06:37:11

标签: ios swift mapbox

我可以在地图上绘制折线路线,但我无法从mapview中清除此路线。我的代码是:

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方法,但它没有帮助。感谢。

3 个答案:

答案 0 :(得分:1)

我找到了解决方案。我们可以使用self.mapView.style?.source(withIdentifier: "polyline")这样的标识符来源,然后将其删除。

答案 1 :(得分:1)

我正在处理几乎相同的问题。我需要清理地图并添加另一条折线。  我创建折线的代码

 let source = MGLShapeSource(identifier: randomIdentifier, shape: nil, options: nil)

    self.mapView.style?.addSource(source)
    self.polylineSource = source

    let layer = MGLLineStyleLayer(identifier: "polyline", source: source)

错误说:

  

“ MGLRedundantSourceIdentifierException”,原因:“源折线   已经存在'。

因此,我发现没有简单的方法可以删除折线。 我创建了一个随机字符串,每当它创建一个新的字符串来解决此问题时,便将其添加到折线的标识符中

func randomString(length: Int) -> String {
    let letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
    return String((0..<length).map{ _ in letters.randomElement()! })
}

...

func drawRoute(route: Route){

    let randomIdentifier = randomString(length: 8)
    print("random identifier \(randomIdentifier)")

    let source = MGLShapeSource(identifier: randomIdentifier, shape: nil, options: nil)

希望这会有所帮助

答案 2 :(得分:0)

这对我有用:

if let source = mapView.style?.source(withIdentifier: "polyline") as? MGLShapeSource {

        source.shape = nil
    }