Dotted line on map view

放肆的年华 提交于 2019-12-05 01:21:05

You can use the lineDashPattern property to create the pattern you want for the line.

MKPolylineRenderer is a subclass of MKOverlayPathRenderer which has that property and a few others (see the link to the documentation).

For example, this sets the pattern to a line 2 points long followed by a 5 point gap. The pattern is repeated for the entire length of the polyline.

renderer.lineDashPattern = @[@2, @5];


For the opacity, you can either apply an alpha to the strokeColor:

renderer.strokeColor = [[UIColor orangeColor] colorWithAlphaComponent:0.5];

or set the alpha property:

renderer.alpha = 0.5;

Not sure what you mean by "the longer the line is" part of the question.

Answer in Swift

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.1764705926, green: 0.4980392158, blue: 0.7568627596, alpha: 1)
    renderer.lineWidth = 8
    renderer.lineDashPattern = [0, 10]

    return renderer
} 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!