MKMultiPolylineRenderer with attributes?

Is there any way to use attributes per polyline with a MKMultiPolylineRenderer?

Or do I have to create a different renderer even if the only difference is line width?

I tried sub-classing MKMultiPolylineRenderer, but I do not see anyway to tie attributes to the CGPath being drawn.

In the code below, draw(mapRect:zoomScale:in:) shows I have 1197 polylines, but strokePath(in:) is called only once per draw call.

If I could somehow tell what path number it was stroking I could bind attributes to the strokePath function. Since draw(mapRect:) only draws one path, I don’t see how I can. If strokePath only provided an index I’d be all set.

I guess I could override the draw function entirely (and not call its super function), but I would be rendering all 1197 paths 1197 times since I don’t know what path it is rendering per call.

class MyMultiPolylineRenderer: MKMultiPolylineRenderer {
    
    //Even though there are 1197 polylines, this is called once per draw(mapRect:)
    override func strokePath(_ path: CGPath, in context: CGContext) {
        super.strokePath(path, in: context)
        print("strokePath")
    }
    
    override func draw(_ mapRect: MKMapRect, zoomScale: MKZoomScale, in context: CGContext) {
        
        //Causes a single call to strokePath
        super.draw(mapRect, zoomScale: zoomScale, in: context)

        var counter = 0
        for poly in self.multiPolyline.polylines {
            counter += 1
        }
        
        //polylines counter: 1197
        print("polylines counter: \(counter)")
    }
}

Leave a Comment