ios - MapBox:在注释上绘制的 MGLineStyleLayer

标签 ios swift mapbox mglmapview

我正在尝试将注释和 MGLineStyleLayer 添加到 mapView。我已成功添加它们,但 LineLayer 绘制在注释上方。我希望在线条图层上绘制注释。这是我的实现

 /*This is where i am adding the markers*/
    func createMapPoints(points: [CLLocationCoordinate2D], dashed: Bool = false) {
        var waypointType: MapAnnotationEnum = .waypointAnnotation
        if dashed {
            waypointType = .offsetWaypoint
        }
        addLineLabel(points: points, width: 7, color: UIColor.white, dashed: dashed)
        for point in points {
            _ = addAnnotationLabel(location: point, title: "WP", type: waypointType)
        }
    }

    func addAnnotationLabel(location: CLLocationCoordinate2D, title: String, type: MapAnnotationEnum) -> CustomMapGLAnnotaion {
        let annotation = CustomMapGLAnnotaion()
        annotation.coordinate = location
        annotation.title = title
        annotation.annotationType = type
        self.mapView.addAnnotation(annotation)
        return annotation
    }

    func addLineLabel(points: [CLLocationCoordinate2D], width: CGFloat, color: UIColor, dashed: Bool = false) {
        let polyline = CustomMapGLPolyline(coordinates: points, count: UInt(points.count))
        polyline.width = width
        polyline.color = color
        shapeCount += 1
        if dashed {
            polyline.title = "dashed"
            addDashedLine(polyline: polyline)
        } else {
            polyline.title = "0"
            addCasingLine(polyline: polyline)
        }
            print("Last Execution Point 3")
            self.mapView.addAnnotation(polyline)
    }

    func addCasingLine(polyline: MGLPolyline) {
        guard let style = self.mapView.style else { return }

        let source = MGLShapeSource(identifier: "line\(shapeCount)", shape: polyline, options: nil)
        style.addSource(source)

        style.layer(withIdentifier: )
        let lineLayer = MGLLineStyleLayer(identifier: "line-layer\(shapeCount)", source: source)
        lineLayer.lineJoin = NSExpression(forConstantValue: "round")
        lineLayer.lineCap = NSExpression(forConstantValue: "round")
        lineLayer.lineColor = NSExpression(forConstantValue: #colorLiteral(red: 0.1254901961, green: 0.4901960784, blue: 0.9137254902, alpha: 1))
        lineLayer.lineOpacity = NSExpression(forConstantValue: 1)
        lineLayer.lineWidth = NSExpression(format: "mgl_interpolate:withCurveType:parameters:stops:($zoomLevel, 'linear', nil, %@)", [14: 4, 18: 4])
        style.addLayer(lineLayer)
    }

    func addDashedLine(polyline: MGLPolyline) {
        guard let style = self.mapView.style else { return }
        let source = MGLShapeSource(identifier: "line\(shapeCount)", shape: polyline, options: nil)
        style.addSource(source)
        let dashedLayer = MGLLineStyleLayer(identifier: "polyline-dash\(shapeCount)", source: source)
        dashedLayer.lineJoin = NSExpression(forConstantValue: "round")
        dashedLayer.lineCap = NSExpression(forConstantValue: "round")
        dashedLayer.lineColor = NSExpression(forConstantValue: #colorLiteral(red: 0.9411764706, green: 0.3764705882, blue: 0.1921568627, alpha: 1))
        dashedLayer.lineOpacity = NSExpression(forConstantValue: 1)
        dashedLayer.lineWidth = NSExpression(format: "mgl_interpolate:withCurveType:parameters:stops:($zoomLevel, 'linear', nil, %@)", [14: 4, 18: 4])
        dashedLayer.lineDashPattern = NSExpression(forConstantValue: [0, 1.5])
        style.addLayer(dashedLayer)
    }


这是两个自定义类的定义

  class CustomMapGLAnnotaion: MGLPointAnnotation {

    var annotationType: MapAnnotationEnum?
    override init() {
        super.init()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    }

   class CustomMapGLPolyline: MGLPolyline {

    var width: CGFloat?
    var color: UIColor?
    override init() {
        super.init()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
  }

我哪里错了?感谢您的帮助!

最佳答案

对于将来寻找答案的人!

由于 MGLAnnotationView 继承自 UIView 对象。我们可以简单地通过设置其 zIndex 属性来决定注释功能的顺序。


根据https://docs.mapbox.com/ios/maps/overview/markers-and-annotations/

关于ios - MapBox:在注释上绘制的 MGLineStyleLayer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54806259/

相关文章:

ios - 如何在 typescript 中的函数内定义函数?

ios - 将 Eureka 表单数据上传到 firebase

iOS - 链接具有比我的项目更高的部署目标的框架

javascript - Mapbox JS GL 中的数据驱动线宽

reactjs - 使用 React 缺少 MapBox CSS

javascript - 如何在混合应用程序中调试 onDeviceReady -- cordova

ios - "unrecognized selector sent to instance"用推文创建 NSDictionary 时

带有字符串和 bool 值的 JSON 数组

ios - receiveCompletion 和 receiveValue 未被调用

mapbox - 如何在 iOS 上为 Mapbox 动态更新 MGLShapeSource 的 MGLPointFeature 属性值?