如何在 swift 4 中添加虚折线?

How do i add dashed polyline in swift 4?

我在 Swift4 中已经有一条折线,我怎样才能将它的样式设置为 Dashed/Dotted 行??

  let   path = GMSMutablePath(path: GMSPath())
    for marker in markerList {
        bounds = bounds.includingCoordinate(marker.position)
        path.add(marker.position)

    }



    let polyline = GMSPolyline(path: path)
    polyline.strokeColor = UIColor(named: "Primary")!
    polyline.strokeWidth = 3.0
    polyline.geodesic = true
    polyline.map = mapView

Like this map

您需要实施 func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer 委托方法并使用 renderer.lineDashPhaserenderer.lineDashPattern 属性,您应该能够实现您需要的东西

代码示例

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
    let renderer = MKPolylineRenderer(overlay: overlay)
    renderer.strokeColor = UIColor.red
    renderer.lineWidth = 4.0
    renderer.lineDashPhase = 2
    renderer.lineDashPattern = [NSNumber(value: 1),NSNumber(value:5)]
    return renderer
}

结果