Reputation: 1644
I'm trying to animate a series of radar images over a map of the US. I have a list of radar gif images downloaded from the NOAA website.
How can I implement an animation of a series of images on a mapkit Overlay? I've seen these posts here: Animated MKOverlayView and Animating an MKOverlayView
but couldn't find a solution.
Upvotes: 1
Views: 1879
Reputation: 606
SWIFT
Add image as overlay by subclassing MKOverlay and MKOverlarRenderer
MapOverlay.swift
import UIKit
import MapKit
class MapOverlay: NSObject, MKOverlay {
var coordinate: CLLocationCoordinate2D
var boundingMapRect: MKMapRect
init(coord: CLLocationCoordinate2D, rect: MKMapRect) {
self.coordinate = coord
self.boundingMapRect = rect
}
}
MapOverlayView.swift
import UIKit
import MapKit
class MapOverlayView: MKOverlayRenderer {
var overlayImage: UIImage
init(overlay: MKOverlay, overlayImage:UIImage) {
self.overlayImage = overlayImage
super.init(overlay: overlay)
}
override func draw(_ mapRect: MKMapRect, zoomScale: MKZoomScale, in context: CGContext) {
let mapImage = overlayImage.cgImage
let mapRect = rect(for: overlay.boundingMapRect)
context.scaleBy(x: 1.0, y: -1.0)
context.translateBy(x: 0.0, y: -mapRect.size.height)
context.draw(mapImage!, in: mapRect)
}
}
ViewController.swift
import UIKit
import MapKit
class ViewController: UIViewController {
@IBOutlet weak var mapview: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
mapview.delegate = self
let location = CLLocationCoordinate2D(latitude: 47.6062, longitude: -122.3320)
let span = MKCoordinateSpanMake(2.0, 2.0)
let region = MKCoordinateRegion(center: location, span: span)
mapview.setRegion(region, animated: true)
let rec = should be same as your image size
let overlay = MapOverlay(coord: location, rect: rec)
mapview.add(overlay)
}
}
extension ViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
if overlay is MapOverlay {
let logo = UIImage(named: "swift")
let overlayView = MapOverlayView(overlay: overlay, overlayImage: logo)
return overlayView
} else {
return MKPolylineRenderer()
}
}
}
After this call a function to toggle alpha value of MKOverlayRenderer.
The project is available in github : link
Upvotes: 0
Reputation: 1644
I solved the problem by creating a timer in my view controller. Each time the timer fires it calls the setNeedsDisplay method for my custom MKOverlayRenderer. In the renderer subclass drawMapRect method, I have the following code to update the image on the overlay:
-(void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context{
if (!weatherDataStore) //This is the store where I have my radar images
weatherDataStore = [WeatherDataStore sharedInstance];
UIImage *image = [weatherDataStore currentRadarImage];
if (!image || ![image isKindOfClass:[UIImage class]]) {
return;
}
CGImageRef imageReference = image.CGImage;
CGContextSetAlpha(context, 0.8);
MKMapRect theMapRect = [self.overlay boundingMapRect];
CGRect theRect = [self rectForMapRect:theMapRect];
CGContextScaleCTM(context, 1.0, -1.0);
CGContextTranslateCTM(context, 0.0, -theRect.size.height);
CGContextDrawImage(context, theRect, imageReference);
}
Upvotes: 0