3

i am having trouble to add two different MKPolylineView with diferent colors in a MKOverlayView. Any ideas on how to achieve this? Thanks

Here is my code:

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
MKOverlayView* overlayView = nil;
UIColor *mycolor;

self.routeLineView = [[[MKPolylineView alloc] initWithPolyline:self.routeLine] autorelease];

mycolor = [UIColor colorWithRed:85.0/255.0 green:133.0/255.0 blue:255.0/255.0 alpha:0.6];
self.routeLineView.fillColor = mycolor;
self.routeLineView.strokeColor = mycolor;
self.routeLineView.lineWidth = 15;
[overlayView addSubview:self.routeLineView];

self.routeLineView2 = [[[MKPolylineView alloc] initWithPolyline:self.routeLine2] autorelease];
mycolor = [UIColor colorWithRed:85.0/255.0 green:19.0/255.0 blue:25.0/255.0 alpha:0.6];
self.routeLineView2.fillColor = mycolor;
self.routeLineView2.strokeColor = mycolor;
self.routeLineView2.lineWidth = 15;
[overlayView addSubview:self.routeLineView2];   

return overlayView;
}
pochimen
  • 287
  • 3
  • 15

1 Answers1

0

The viewForOverlay method will be called separately for each overlay you add to the map. So in that method, you return just the overlay view for the overlay that it is currently being called for (ie. the overlay parameter).

Check which overlay it is requesting a view for and create and return a view only for that overlay.

For example:

if (overlay == self.routeLine)
{
    //create and return overlay view for routeLine...
    //set color, etc...
    return self.routeLineView;
}
else
if (overlay == self.routeLine2)
{
    //create and return overlay view for routeLine2...
    //set color, etc...
    return self.routeLineView2;
}

return nil;

Don't do any addSubview stuff. Just create the overlay view and return it.