8

Can any help me with making custom MKPolyline with additional argument Color?

CustomPolyline.swift

import Foundation
import MapKit
class CustomPolyline : MKPolyline {
    let coordinates: UnsafeMutablePointer<CLLocationCoordinate2D>
    let count : Int = 0
    let color : String = "ff0000"
    init(coordinates: UnsafeMutablePointer<CLLocationCoordinate2D>, count: Int, color: String) {

        self.coordinates = coordinates
        self.count = count
        self.color = color
    }
}

Init

Polyline = CustomPolyline(coordinates: &Path, count: Path.count, color: "ff0000")
self.mapView.addOverlay(Polyline)

func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {      
        if (overlay is CustomPolyline) {
            var pr = MKPolylineRenderer(overlay: overlay);
            pr.strokeColor = UIColor.colorWithRGBHex(0xff0000).colorWithAlphaComponent(0.5);
            pr.lineWidth = 10;
            return pr;
        }

        return nil
    }

My solution doesn't work and I can't figure it out why. Polylines isn't visible at all. I'm a beginner in SWIFT so I think that problem is with my CustomPolyline class. Thanks for help.

Rajesh
  • 10,318
  • 16
  • 44
  • 64
seek
  • 1,065
  • 16
  • 33

3 Answers3

13

It can be done much more simpler than I throught:

Class

import Foundation
import MapKit

class CustomPolyline : MKPolyline {

    var color: String?
}

Init

cPolyline = CustomPolyline(coordinates: &Path, count: Path.count)
cPolyline.color = "#ff0000"
self.mapView.addOverlay(cPolyline)

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

            var pr = MKPolylineRenderer(overlay: overlay);
            pr.strokeColor = UIColor(rgba: overlay.color);
            pr.lineWidth = 10;
            return pr;

    }
seek
  • 1,065
  • 16
  • 33
  • I am having trouble with MKPolyline, specifically in passing in the UnsafeMutablePointer. How is that constructed? What is the type of the Path variable you are passing in? – Alice Isabelle Oct 16 '14 at 06:53
  • Path is simple CLLocationCoordinate2D array var Path:[CLLocationCoordinate2D] = [locationOne,locationTwo] – seek Oct 22 '14 at 15:49
  • 2
    changing the renderForOverlay type from the default `MKOverlay` to `CustomPolyLine` makes it so nothing is drawn, any suggestions? – Tyler Kelly May 18 '17 at 16:47
  • Not working for me. I get nothing drawn. Xcode asks to make the delegate function private if I replace MKOverlay to CustomPolyLine. Obv Xcode doesn't see that as delegate function anymore. Casting did not work either... – multitudes Jul 27 '21 at 12:37
5

As user3470987 pointed out, there might be an issue in modifying the default 'rendererForOverlay' delegate function. It could also prove difficult if you'd like to add overlays of other types than MKPolyline.

Class

import Foundation
import MapKit

class Polyline: MKPolyline {
    var color: UIColor?
}

Render function

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {

    if let polyline = overlay as? Polyline {

        let polylineRenderer = MKPolylineRenderer(overlay: polyline)
        polylineRenderer.strokeColor = polyline.color
        polylineRenderer.lineWidth = 3
        return polylineRenderer
    }

    return MKOverlayRenderer(overlay: overlay)

}
nomadoda
  • 4,561
  • 3
  • 30
  • 45
  • This should be the accepted answer? It is crucial to get the signature of the delegate function correct otherwise it will not draw anything! – multitudes Jul 27 '21 at 12:44
1

Another solution most easy:

extension UIColor {
    static var rendererColor: UIColor {
        return UIColor(red: 65/255, green: 65/255, blue: 65/255, alpha: 1)
    }
}

And the use:

renderer.strokeColor = UIColor.renderer
LAD
  • 135
  • 5
  • 13