0

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

matt
  • 447,615
  • 74
  • 748
  • 977
MarBVI
  • 591
  • 2
  • 8
  • 32
  • You should invest some time to understand what this example is doing. It's really straightforward and easy and it's not hard to achieve what you want – JustSid Feb 03 '14 at 01:30
  • Unless I'm misunderstanding, it's not exactly "straightforward and easy". Using a different color for each route could be done by setting each polyline's subtitle (which MKDirections doesn't seem to use) to some "color code" and then setting the color based on that value in rendererForOverlay. –  Feb 03 '14 at 03:28
  • Detecting a touch on a route is more difficult. [This answer](http://stackoverflow.com/a/20425540/467105) by @Jensemann works well. Changing the color once a route is touched can be done by removing that overlay, changing its "color code", then adding the overlay back. –  Feb 03 '14 at 03:30
  • @Anna You should give that explanation as an answer. It's basically what I was going to say, but you've described it first so you should write the answer. He needs to understand that he must supply a way so that the renderer can distinguish which color it should draw in. – matt Feb 03 '14 at 03:39
  • What I would do is create 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. Later, the renderer can fetch that color and use it to set the MKPolylineRenderer's stroke color. – matt Feb 03 '14 at 03:48
  • @matt, That's a better approach than the crude hack I described. You're welcome to post _that_ as an answer. –  Feb 03 '14 at 03:53
  • Well, this seems much more difficult that i expected...will try your answers. Thank you very much for help! Will be in touch. Regards for both @Anna and matt – MarBVI Feb 03 '14 at 15:17

2 Answers2

3

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;
matt
  • 447,615
  • 74
  • 748
  • 977
2

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.

Community
  • 1
  • 1
  • 1
    In iOS 7 unfortunately there is no "overlay view". But you could create a custom overlay object conforming to MKOverlay and a custom overlay renderer, and this is not at all difficult. This project contains an example of doing that: https://github.com/mattneub/Programming-iOS-Book-Examples/tree/master/bk2ch21p744maps/ch34p1008maps – matt Feb 03 '14 at 04:07