7

this is my code for drawing first Line, for another line with another color how can i do ?????

 func mapView(mapView : MKMapView! , rendererForOverlay overlay: MKOverlay!) ->MKOverlayRenderer! {

    if overlay is MKPolyline {

        var polyLineRenderer = MKPolylineRenderer(overlay: overlay)
        polyLineRenderer.strokeColor = UIColor.blackColor()
        polyLineRenderer.lineWidth = 10

        return polyLineRenderer
    }
    return nil
  }
Paul
  • 848
  • 12
  • 33
  • Try NSColor.redColor().set – qwerty_so Jan 20 '15 at 12:33
  • 1
    Try this: http://stackoverflow.com/questions/26010977/how-to-make-custom-mkpolyline-in-swift-with-additional-argument-color. But instead of declaring the custom `color` property as a `String?`, I think it would be simpler to declare it as a `UIColor?`. –  Jan 20 '15 at 13:42
  • In that post you show me Anna , there is a property with name Color in Objective-c ,but i couldn't find it in swift –  Jan 20 '15 at 23:00
  • That's right. The post is saying to subclass MKPolyline and add a custom property named color. Your approach will work too. One drawback is that you have to keep references to each polyline and the delegate method has to reference them which makes it less self-contained than it could be. –  Jan 20 '15 at 23:04

3 Answers3

8

Finally i found the way :

In the top of the class i put

var toGo    : MKPolyline?
var toCome  : MKPolyline?

and after that in view Did load :

                    var polyLineGoes = MKPolyline(coordinates: &coordinateGoes, count: coordinateGoes.count)
                    toGo = polyLineGoes
                    mapView.addOverlay(polyLineGoes)


                    var polyLineComes = MKPolyline(coordinates: &coordinateComes, count: coordinateComes.count)
                    toCome = polyLineComes
                    mapView.addOverlay(polyLineComes)

at the End of class :

func mapView(mapView : MKMapView! , rendererForOverlay overlay: MKOverlay!) ->MKOverlayRenderer! {

    if overlay is MKPolyline {
        if ( toGo  != nil) && (toCome != nil ) {
            if overlay as? MKPolyline  == toGo {
                var polyLineRenderer = MKPolylineRenderer(overlay: overlay)
                polyLineRenderer.strokeColor = UIColor.redColor()
                polyLineRenderer.lineWidth = 3

                return polyLineRenderer
            } else if overlay as? MKPolyline  == toCome {
                print(overlay.description)
                var polyLineRenderer = MKPolylineRenderer(overlay: overlay)
                polyLineRenderer.strokeColor = UIColor.blueColor()
                polyLineRenderer.lineWidth = 3

                return polyLineRenderer
            }
        }
    }
    return nil

}
  • I found this useful, though this post is now outdated due to Swift changing somewhat. "return nil" can't be done anymore, see this Q&A for more detail: https://stackoverflow.com/questions/33102748/nil-is-incompatible-with-return-type-mkoverlayrenderer – prince May 05 '20 at 18:20
2

I was facing a same problem, but I just find another way to solve it.

In the mapView the code will be:

    func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer!{
        if overlay is MKPolyline {
            var polylineRenderer = MKPolylineRenderer(overlay: overlay)
            polylineRenderer.strokeColor = variables.lineColor
            polylineRenderer.lineWidth = 4
            return polylineRenderer
        }
        return nil
    }

and I just have a struct named variables, and it contains variable lineColor. Also, I have another CONSTANT struct it has some CONSTANT to make code in mapView class more readable. The code in these two structs will be:

    struct CONSTANT {
        static let greenColor = UIColor.greenColor().colorWithAlphaComponent(0.5)
        static let blueColor = UIColor.blueColor().colorWithAlphaComponent(0.5)
        static let redColor = UIColor.redColor().colorWithAlphaComponent(0.5)
    }
    
    struct variables {
        // let the default lineColor be green
        static var lineColor = CONSTANT.greenColor
    }

With these, I will just change my variables.lineColor to the color in CONSTANT struct. For example,

lineColor = CONSTANT.greenColor
mapView.addOverlay(myPolyLine, level: MKOverlayLevel.AboveRoads)

lineColor = CONSTANT.blueColor
mapView.addOverlay(myPolyLine, level: MKOverlayLevel.AboveRoads)

lineColor = CONSTANT.redColor
mapView.addOverlay(myPolyLine, level: MKOverlayLevel.AboveRoads)

First post here :D

h01m3s
  • 290
  • 2
  • 5
2

I have found yet another way.

First, we need to extend MKPolyline:

extension MKPolyline {
    struct ColorHolder {
        static var _color: UIColor?
    }
    var color: UIColor? {
        get {
            return ColorHolder._color
        }
        set(newValue) {
            ColorHolder._color = newValue
        }
    }
}

In viewDidLoad, we can assign every polyline a color now:

var polyline1 = MKPolyline(coordinates: coordinates1, count: coordinates1.count)
polyline1.color = UIColor(.red)
mapView.addOverlay(polyline1)

var polyline2 = MKPolyline(coordinates: coordinates2, count: coordinates2.count)
polyline2.color = UIColor(.green)
mapView.addOverlay(polyline2)

Finally, we have our mapView-function:

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
    if let overlay_ = overlay as? MKPolyline {
        let renderer = MKPolylineRenderer(overlay: overlay)
        renderer.strokeColor = overlay_.color
        return renderer
    } else {
        return MKOverlayRenderer()
    }
}
Paul
  • 848
  • 12
  • 33