Clip
Clip

Reputation: 3078

Animated Radar Overlay MKMapView iOS

I am fetching a gif from the NOAA of the current weather radar, I store it in a UIImage and then draw it to an MKMapView, here is some of the code I am using:

image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://radar.weather.gov/ridge/Conus/RadarImg/latest_radaronly.gif"]]];

MKMapRect theMapRect    = [self.overlay boundingMapRect];
CGRect theRect           = [self rectForMapRect:theMapRect];

@try {
    UIGraphicsBeginImageContext(image.size);
    UIGraphicsPushContext(ctx);
    [image drawInRect:theRect];
    UIGraphicsPopContext();
    UIGraphicsEndImageContext();
}
@catch (NSException *exception) {
    NSLog(@"Caught an exception while drawing radar on map - %@",[exception description]);
}
@finally {

}

Does anyone know how I can animate this gif repeatedly to achieve a weather like radar you see on the news. I have found several apps that do this and am wondering how I can incorporate it into my own project.

Upvotes: 2

Views: 1074

Answers (1)

Daniel Storm
Daniel Storm

Reputation: 18898

The link you provided doesn't actually show an animated gif. It just retrieves the latest gif. One way you could do it is load the latest 10-20 gif's the NOAA has uploaded here, they seem to update it every 10 minutes, then create a UIImage for each gif and cycle through them in a UIImageview which overlays your map. For example:

// Set images
radarGIF01 = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://radar.weather.gov/ridge/Conus/RadarImg/Conus_20150209_0108_N0Ronly.gif"]]];
radarGIF02 = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://radar.weather.gov/ridge/Conus/RadarImg/Conus_20150209_0118_N0Ronly.gif"]]];
radarGIF03 = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://radar.weather.gov/ridge/Conus/RadarImg/Conus_20150209_0128_N0Ronly.gif"]]];
radarGIF04 = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://radar.weather.gov/ridge/Conus/RadarImg/Conus_20150209_0138_N0Ronly.gif"]]];
radarGIF05 = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://radar.weather.gov/ridge/Conus/RadarImg/Conus_20150209_0148_N0Ronly.gif"]]];

// Add images to array
radarImagesArray = [[NSArray alloc]initWithObjects: radarGIF01, radarGIF02, radarGIF03, radarGIF04, radarGIF05, nil];

// Animate images in UIImageview
_radarImageView.animationImages = radarImagesArray;
_radarImageView.animationDuration = 3.0;
_radarImageView.animationRepeatCount = 0;
[_radarImageView startAnimating];

Upvotes: 2

Related Questions