3

I am drawing x amount of lines on an MKMapView. The data is being downloaded from a webservice and for each line that needs drawing I am creating a MKPolyline, adding it to an overlayArray (some overlays have more than one polyline) and finally adding that overlay to the map via:

[self.mapView addOverlays:overlayArray];`

The next function is therefore called which works wonderfully:

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
    if (![overlay isKindOfClass:[MKPolygon class]]) {
        MKPolyline *route = overlay;
        MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithPolyline:route];
        renderer.strokeColor = [lineColours objectForKey:route.title];
        renderer.lineWidth = 3.0;
        return renderer;
    } else {
        return nil;
    }
}

As you can see, the strokeColor gets the correct UIColor from a pre-defined NSMutableDictionary. This all works as expected.

However, some of the lines overlap and using a single colour is not always desired. I was wondering if there was a way of creating a line made up of two or even 3 colours, but not a gradient along the line as seen in many fitness apps, but colours across the line. For example, another use would be to draw a motorway route made up of two white lines with a transparent strip in the middle.

This is quite important for an app I am developing so I will post any findings I find here, and I hope that other people can share their thoughts and knowledge too.

Patrick
  • 6,495
  • 6
  • 51
  • 78
  • 1
    MKOverlayPathRenderer (which MKPolylineRenderer is a subclass of) has some properties to customize the path (lineDashPattern, etc) but they may not be enough for you. You'll probably need to create a custom MKOverlayPathRenderer subclass and do the drawing yourself. See [this answer](http://stackoverflow.com/questions/7767580/how-to-customize-mkpolylineview-to-draw-different-style-lines) for an example. It's subclassing MKOverlayPathView (the old, deprecated version of the new MKOverlayPathRenderer) but the implementation is basically the same. –  Jan 11 '14 at 03:31

0 Answers0