Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i add dashed polyline in swift 4?

i already have a polyline in Swift4, how can i make the style of it to Dashed/Dotted line ??

  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

like image 963
mohamed ibrahem Avatar asked Mar 07 '23 15:03

mohamed ibrahem


1 Answers

You need to implement func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer delegate method and using renderer.lineDashPhase and renderer.lineDashPattern properties you should be able to achive what you need

code example

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
}

result

enter image description here

like image 143
Reinier Melian Avatar answered Mar 15 '23 06:03

Reinier Melian