有没有办法在 mkmapview 中将虚线绘制为多段线?

Is there a way to draw a dotted curve as a polyline in mkmapview?

我目前正在使用 curvyRoute 库来绘制弯曲的多边形线条。它将折线绘制为普通线,但我希望折线被点缀。该库使用 addquadcurve 绘制曲线。有什么办法可以让它变成虚线吗?

您可以通过添加此样式来绘制虚线。

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
}