title: MKOverlayView and OpenGL
body: <p>I currently have a <code>UIView</code> that draws radar data on top of a <code>MKMapView</code> using OpenGL. Because of the level of detail in the radar image, OpenGL is required (CoreGraphics is not fast enough).</p>
  
  <p>All of the images that I am drawing are saved in <code>MKMapPoints</code>.  I choose them over the standard <code>CLLocationCoordinate2D</code> because their lengths do not depend on the latitude. The basic method for drawing is this:</p>
  
  <ol>
  <li>Add the <code>GLView</code> as a subview of the <code>MKMapView</code> and set <code>GLView.frame = MKMapView.frame</code>.</li>
  <li><p>Using <code>GLOrthof</code>, set the projection of the <code>GLView</code> to equal the current visible <code>MKMapRect</code> of the map.  Here is the code that does this.</p>
  
  <pre><code>CLLocationCoordinate2D coordinateTopLeft =
      [mapView convertPoint:CGPointMake(0, 0)
               toCoordinateFromView:mapView];
  MKMapPoint pointTopLeft = MKMapPointForCoordinate(coordinateTopLeft);
  
  CLLocationCoordinate2D coordinateBottomRight =
      [mapView convertPoint:CGPointMake(mapView.frame.size.width,
                                        mapView.frame.size.height)
               toCoordinateFromView:mapView];
  MKMapPoint pointBottomRight = MKMapPointForCoordinate(coordinateBottomRight);
  
  glLoadIdentity();
  glOrthof(pointTopLeft.x, pointBottomRight.x,
           pointBottomRight.y, pointTopLeft.y, -1, 1);
  </code></pre></li>
  <li><p>Set the viewport to be the correct size using <code>glViewport(0, 0, backingWidth, backingHeight)</code> where <code>backingWidth</code> and <code>backingHeight</code> is the size of the <code>mapView</code> in points.</p></li>
  <li>Draw using <code>glDrawArrays</code>.  Not sure if this matters, but <code>GL_VERTEX_ARRAY</code> and <code>GL_TEXTURE_COORD_ARRAY</code> are both enabled during the draw.</li>
  </ol>
  
  <p>Using this method, everything works fine.  The drawing is performed like it is supposed to.  The only problem is since it is a subview of the <code>mapView</code> (and not an overlay), the radar image is drawn on top of any other <code>MKAnnotations</code> and <code>MKOverlays</code>.  I need this layer to be drawn under the other annotations and overlays.</p>
  
  <p>What I tried to do to get this working was to make the <code>glView</code> a subview of a custom <code>MKOverlayView</code> instead of the <code>mapView</code>.  What I did was give the <code>MKOverlay</code> a <code>boundingMapRect</code> of <code>MKMapRectWorld</code> and set the frame of the <code>glView</code> the same way that I set the projection (since frame of a <code>MKOverlayView</code> is determined by <code>MKMapPoints</code> and not <code>CGPoints</code>).  Again, here is the code.</p>
  
  <pre><code>CLLocationCoordinate2D coordinateTopLeft =
      [mapView convertPoint:CGPointMake(0, 0)
               toCoordinateFromView:mapView];
  MKMapPoint pointTopLeft = MKMapPointForCoordinate(coordinateTopLeft);
  
  CLLocationCoordinate2D coordinateBottomRight =
      [mapView convertPoint:CGPointMake(mapView.frame.size.width,
                                        mapView.frame.size.height)
               toCoordinateFromView:mapView];
  MKMapPoint pointBottomRight = MKMapPointForCoordinate(coordinateBottomRight);
  
  glRadarView.frame = CGRectMake(pointTopLeft.x, pointTopLeft.y,
                                 pointBottomRight.x - pointTopLeft.x,
                                 pointBottomRight.y - pointTopLeft.y);
  </code></pre>
  
  <p>When I do this, the <code>glView</code> is positioned correctly on the screen (in the same place that is was while it was a subview of the <code>mapView</code>), but the drawing no longer works correctly.  When the image does come up, it is not the right size and not in the correct location.  I did a check and <code>backingWidth</code> and <code>backingHeight</code> are still the size of the view in points (as they should be).</p>
  
  <p>Any idea why this is not working?</p>