【问题标题】:Is there a way to draw a dotted curve as a polyline in mkmapview?有没有办法在 mkmapview 中将虚线绘制为折线?
【发布时间】:2021-09-23 02:14:20
【问题描述】:

我目前正在使用curvyRoute 库来绘制弯曲的折线。它将折线绘制为法线,但我希望折线被点缀。该库使用 addquadcurve 绘制曲线。有没有办法让它变成虚线?

【问题讨论】:

    标签: ios swift mapkit mkmapview


    【解决方案1】:

    你可以通过添加这个样式来绘制虚线。

    polyline = MGLPolyline(coordinates: mapCoordinates, count: UInt(mapCoordinates.count))
    
    source = MGLShapeSource(identifier: "line", shape: polyline, options: nil)
    style.addSource(source)
    
    let dottedLayer = MGLLineStyleLayer(identifier: "dotted-line-layer", source: source)
    dottedLayer.lineColor = NSExpression(forConstantValue: UIColor.darkGray)
    dottedLayer.lineWidth = NSExpression(forConstantValue: 4.0)
    dottedLayer.lineCap = NSExpression(forConstantValue: "round")
    dottedLayer.lineDashPattern = NSExpression(forConstantValue: [0.25, 1.5])
    

    如果我们使用 MKMapView 那么我们可以使用它,

    func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
       guard let polyline = overlay as? MKPolyline else {
           fatalError("Not a MKPolyline")
       }
    
       let renderer = MKPolylineRenderer(polyline: polyline)
    
       renderer.strokeColor = #colorLiteral(red: 0.2, green: 0.5, blue: 0.77, alpha: 1)
       renderer.lineWidth = 6
       renderer.lineDashPattern = [0, 10]
    
       return renderer
    }
    

    【讨论】:

    • 一个叫MGLPolyline的人从哪里来? MGLShapeSource?
    • @ElTomato 我已经更新了我对 MKMapView 的回答。感谢您的通知。
    猜你喜欢
    • 1970-01-01
    • 2017-02-13
    • 1970-01-01
    • 2013-03-13
    • 2018-09-01
    • 2015-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多