Alternative routes on MKDirections

I just followed this tutorial tu draw a route on maps in iOS 7 http://www.techotopia.com/index.php/Using_MKDirections_to_get_iOS_7_Map_Directions_and_Routes

It's great, but i can't find how to draw the alternative routes on a different colour, and change colour when i touch each route. With this code, all routes are drawn with the same colour:

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id < MKOverlay >)overlay
{
      MKPolylineRenderer *renderer = 
            [[MKPolylineRenderer alloc] initWithOverlay:overlay];
      renderer.strokeColor = [UIColor blueColor];
      renderer.lineWidth = 5.0;
      return renderer;
}

Any ideas? Thanks in advice

Answers


Here are some "straightforward and easy" ways to do what you describe:

  1. "How to draw the alternative routes on a different color": Since MKDirections doesn't seem to use the subtitle property of the overlays it creates, you could stuff a "color code" in there before adding the overlay and then in rendererForOverlay, set the color of the polyline based on its subtitle.

    However, as @matt commented and answered, a better approach is to subclass MKPolyline and create an explicit color property.

  2. "Touch each route": Detecting a touch on a route is more difficult. This answer by @Jensemann works well.
  3. "Change colour when I touch each route": Changing the color once a route is touched can be done by removing that overlay, changing its "color code", then adding the overlay back. Another option here is to create a custom overlay view (renderer in iOS 7) whose color can be dynamically changed.

Warning: I have not tried this; I'm just typing it right into the page here without running it, a thing I rarely do. So it might not work.

What I would suggest is an MKPolyline subclass that has a color property. When I want to add a polyline as an overlay to the map based on a direction segment, I would instantiate that subclass and give it a color. This is tricky but I think it can be done:

MKPolyline* poly = route.polyline;
MyColoredPolyline* poly2 = 
    [MyColoredPolyline polylineWithPoints:poly.points count:poly.pointCount];
poly2.color = // whatever
[self.map addOverlay:poly2];

Later, the renderer can fetch that color and use it to set the MKPolylineRenderer's stroke color.

if ([overlay isKindOfClass:[MyColoredPolyline class]]) {
    v = [[MKPolylineRenderer alloc] initWithPolyline:(MKPolyline*)overlay];
    v.strokeColor = ((MyColoredPolyline*)overlay).color;

Need Your Help

how to uninstall all python packages

python numpy matplotlib uninstall

I just updated numpy and it seems that there is a conflict between matplotlib and the newest version of numpy. I have matplotlib 1.1.1rc. I tried to upgrade matplotlib using the following command:

Horizontally and Vertically center text for TextView in Android

android android-layout android-intent text textview

How do I center the text horizontally and vertically for a TextView in Android?