代码之家  ›  专栏  ›  技术社区  ›  sametbilgi

地图框清除管线多段线

  •  1
  • sametbilgi  · 技术社区  · 7 年前

    我可以在地图上绘制多段线路线,但无法从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 回复  |  直到 7 年前
        1
  •  2
  •   sametbilgi    7 年前

    我找到了解决办法。我们可以通过以下标识符访问源 self.mapView.style?.source(withIdentifier: "polyline") 然后将其移除。

        2
  •  2
  •   Gabo Ruiz    5 年前

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

     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)
    

    希望这有帮助

        3
  •  1
  •   S.S.D Mohamed Dev    6 年前

    这对我很有用:

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