title: Adding multiple overlays to MKMapView
body: <p>I am having some trouble understanding how to add multiple overlays to MKMapView. </p>
  
  <p>I have an array routes tracked. The routes tracked are arrays of CLLocations. So I have an array of arrays. </p>
  
  <p>I am able to take one route and draw it on the map view. However, I need to draw ALL the routes onto the map view. </p>
  
  <p>So here is how I go about creating the MKPolyline for 1 route: </p>
  
  <pre><code>-(void) loadRoute
  {
      //So we grab an array that holds all the locations of one route. 
              NSArray *locations = [[NSArray alloc] initWithArray:[routesArray objectAtIndex:0]];
  
              // while we create the route points, we will also be calculating the bounding box of our route
              // so we can easily zoom in on it. 
              MKMapPoint northEastPoint; 
              MKMapPoint southWestPoint; 
  
              // create a c array of points. 
              MKMapPoint* pointArr = malloc(sizeof(CLLocationCoordinate2D) *[locations count]);
  
              for(int idx = 0; idx &lt; [locations count]; idx++)
              {
                  CLLocation *tempLoc = (CLLocation*)[locations objectAtIndex:idx];
  
                  CLLocationDegrees latitude  = tempLoc.coordinate.latitude;
                  CLLocationDegrees longitude = tempLoc.coordinate.longitude;
  
                  // create our coordinate and add it to the correct spot in the array 
                  CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude, longitude);
  
                  MKMapPoint point = MKMapPointForCoordinate(coordinate);
  
                  // if it is the first point, just use them, since we have nothing to compare to yet. 
                  if (idx == 0) {
                      northEastPoint = point;
                      southWestPoint = point;
                  }
                  else 
                  {
                      if (point.x &gt; northEastPoint.x) 
                          northEastPoint.x = point.x;
                      if(point.y &gt; northEastPoint.y)
                          northEastPoint.y = point.y;
                      if (point.x &lt; southWestPoint.x) 
                          southWestPoint.x = point.x;
                      if (point.y &lt; southWestPoint.y) 
                          southWestPoint.y = point.y;
                  }
                  pointArr[idx] = point;
              }
  
  
              self.routeLine = [MKPolyline polylineWithPoints:pointArr count:[locations count]];
  
  //Zoom in to fit route on screen
              _routeRect = MKMapRectMake(southWestPoint.x, southWestPoint.y, northEastPoint.x - southWestPoint.x, northEastPoint.y - southWestPoint.y);
  
              // clear the memory allocated earlier for the points
              free(pointArr);
  
  }
  </code></pre>
  
  <p>Here is the MapKit delegate call that returns the overlay: </p>
  
  <pre><code>#pragma mark MKMapViewDelegate
  
  - (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id &lt;MKOverlay&gt;)overlay
  {
      MKOverlayView* overlayView = nil;
  
      if(overlay == self.routeLine)
      {
          //if we have not yet created an overlay view for this overlay, create it now. 
          if(nil == self.routeLineView)
          {
              self.routeLineView = [[MKPolylineView alloc] initWithPolyline:self.routeLine];
              self.routeLineView.fillColor = [UIColor redColor];
              self.routeLineView.strokeColor = [UIColor redColor];
              self.routeLineView.lineWidth = 3;
          }
          overlayView = self.routeLineView;
      }
  
      return overlayView;
  }
  </code></pre>
  
  <p>So all the code given above works fine. I can't figure out how to load more overlays to show the other routes. </p>
  
  <p>I thought that maybe in loadRoute method, running through all the routes and creating a MKOverlayView for each one. Storing all those MKOverlayView in an array then doing: 
  [self.mapView addOverlays:array];</p>
  
  <p>But it doesn't work. The problem is that in the delegate method I somehow need to know which overlay to return. </p>
  
  <p>So if I could do something along the lines of: </p>
  
  <pre><code>- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id &lt;MKOverlay&gt;)overlay
  {
      int tag = overlay.tag;
      return (MKOverlayView*)[arrayOfViews objectAtIndex:tag];
  }
  </code></pre>
  
  <p>it would work fine. But unfortunately it doesn't work like that :(</p>
  
  <p>Any help would be very much appreciated! </p>
  
  <p>EDIT:</p>
  
  <p>I use this in ViewDidLoad to add the overlay:</p>
  
  <pre><code>[self loadRoute];
  
          // add the overlay to the map
          if (nil != self.routeLine) {
              [self.mapView addOverlay:self.routeLine];
          }
  
          // zoom in on the route. 
          [self zoomInOnRoute];
  </code></pre>
  
  <p>This is in the header:</p>
  
  <pre><code>// the data representing the route points. 
      MKPolyline* _routeLine;
  
      // the view we create for the line on the map
      MKPolylineView* _routeLineView;
  
      // the rect that bounds the loaded points
      MKMapRect _routeRect;
  </code></pre>