title: XCode Weather Radar Map Overlay on Mapkit
body: <p>I cant seem to figure this out with loading a custom map overlay from a url. As you can see its load the weather overlay from a URL that adds it to the mapview but it doesnt seem to be working and I cant see why it isnt working. Any help is appreciated.</p>
  
  <p>radarView.h</p>
  
  <pre><code>#import &lt;UIKit/UIKit.h&gt;
  #import &lt;MapKit/MapKit.h&gt;
  
  @interface radarViewController : UIViewController &lt;MKMapViewDelegate&gt; {
  MKMapView *mapview;
  }
  @property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
  @property (nonatomic, readwrite) MKCoordinateRegion region;
  @property (nonatomic, readonly) MKMapRect visibleRect;
  @property (nonatomic, retain) IBOutlet MKMapView *mapview;
  
  -(IBAction)setMap:(id)sender;
  -(IBAction)goBack;
  
  @end
  </code></pre>
  
  <p>radarView.m</p>
  
  <pre><code>#import "radarViewController.h"
  #import "MapOverlay.h"
  #import "MapOverlayView.h"
  
  @implementation radarViewController
  @synthesize mapview,coordinate,visibleRect,region;
  
  - (void)viewDidLoad
  {
  [super viewDidLoad];
  [mapview setDelegate:self];
  [mapview setZoomEnabled:YES];
  [mapview setScrollEnabled:YES];
  [mapview setMapType:MKMapTypeSatellite];
  
  NSURL *imageURL = [NSURL URLWithString: @"http://radar.weather.gov/ridge/Conus/RadarImg/latest_radaronly.gif"];
  NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
  //UIImage * image = [UIImage imageWithData:imageData];
  
  MapOverlay *overlay = [[MapOverlay alloc] initWithImageData:imageData withLowerLeftCoordinate:CLLocationCoordinate2DMake(21.65253888888889, -129.314525) withUpperRightCoordinate:CLLocationCoordinate2DMake(50.406625, -65.60158888888888)];
  [mapview addOverlay:overlay];
  [mapview setVisibleMapRect:[overlay boundingMapRect]];
  [overlay release];
  
  visibleRect = [mapview mapRectThatFits:overlay.boundingMapRect];
  mapview.visibleMapRect = visibleRect;
  region = MKCoordinateRegionForMapRect(visibleRect);
  region.center.latitude = 26.503292;
  region.center.longitude = -82.032353;
  region.span.longitudeDelta = 0.4f;
  region.span.latitudeDelta = 0.4f;
  [mapview regionThatFits:region];
  [mapview setRegion:region animated:YES];
  [mapview setClipsToBounds:true];
  
  }
  
  - (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id &lt;MKOverlay&gt;)overlay {
  MapOverlay *mapOverlay = overlay;
  MapOverlayView *mapOverlayView = [[[MapOverlayView alloc] initWithOverlay:mapOverlay] autorelease];
  return mapOverlayView;
  }
  @end
  </code></pre>
  
  <p>MapOverlay.h and .m</p>
  
  <pre><code>#import &lt;MapKit/MapKit.h&gt;
  
  @interface MapOverlay : NSObject &lt;MKOverlay&gt; {
  MKMapRect boundingMapRect;
  }
  @property (nonatomic, retain) NSData *radarData;
  
  - (id) initWithImageData: (NSData*) imageData withLowerLeftCoordinate:
  (CLLocationCoordinate2D) lowerLeftCoordinate withUpperRightCoordinate:
  (CLLocationCoordinate2D) upperRightCoordinate;
  
  @end
  
  
  
  #import "MapOverlay.h"
  
  @implementation MapOverlay
  @synthesize radarData,coordinate,boundingMapRect;
  
  
  - (id) initWithImageData: (NSData*) imageData withLowerLeftCoordinate: (CLLocationCoordinate2D) lowerLeftCoordinate withUpperRightCoordinate: (CLLocationCoordinate2D) upperRightCoordinate{
  
  self.radarData = imageData;
  
  MKMapPoint lowerLeft = MKMapPointForCoordinate(lowerLeftCoordinate);
  MKMapPoint upperRight = MKMapPointForCoordinate(upperRightCoordinate);
  
  boundingMapRect = MKMapRectMake(lowerLeft.x, upperRight.y, upperRight.x - lowerLeft.x, upperRight.y - lowerLeft.y);
  
  return self;
  }
  
  - (CLLocationCoordinate2D)coordinate
  {
  return MKCoordinateForMapPoint(MKMapPointMake(MKMapRectGetMidX(boundingMapRect),
                                                MKMapRectGetMidY(boundingMapRect)));
  }
  
  - (MKMapRect)boundingMapRect
  {
  return boundingMapRect;
  }
  
  @end
  </code></pre>
  
  <p>MapOverlayView.h and .m</p>
  
  <pre><code>#import &lt;MapKit/MapKit.h&gt;
  
  @interface MapOverlayView : MKOverlayView {
  CGFloat tileAlpha;
  }
  
  @property (nonatomic, assign) CGFloat tileAlpha;
  
  @end
  
  
  #import "MapOverlayView.h"
  #import "MapOverlay.h"
  
  @implementation MapOverlayView
  @synthesize tileAlpha;
  
  - (id)initWithOverlay:(id &lt;MKOverlay&gt;)overlay
  {
  if (self = [super initWithOverlay:overlay]) {
      self.tileAlpha = 0.8;
  }
  return self;
  }
  
  - (void)drawMapRect:(MKMapRect)mapRect
        zoomScale:(MKZoomScale)zoomScale
        inContext:(CGContextRef)context
  {
  MapOverlay *mapOverlay = (MapOverlay *)self.overlay;
  
  CGContextSetAlpha(context, tileAlpha);
  
  UIImage *image = [[UIImage alloc] initWithData:mapOverlay.radarData];
  
  NSString *strData = [[NSString alloc]initWithData:mapOverlay.radarData encoding:NSUTF8StringEncoding];
  NSLog(@"%@",strData);
  
  MKMapRect theMapRect = [self.overlay boundingMapRect];
  CGRect theRect = [self rectForMapRect:theMapRect];
  
  UIGraphicsPushContext(context);
  [image drawInRect:theRect blendMode:kCGBlendModeNormal alpha:1.0];
  UIGraphicsPopContext();
  
  [image release]; 
  }
  
  - (BOOL)canDrawMapRect:(MKMapRect)mapRect
           zoomScale:(MKZoomScale)zoomScale
  {
  return YES;
  }
  
  @end
  </code></pre>