0

Im trying to display a NOAA radar image into my MapView using a subclassed MKOverlay in conjunction with a subclassed MKOverlayRenderer. Ive looked at several other links and have tried several methods but I either dont get any image at all (because the mapRect isnt proper) or the image that I do get is stretched up into Canada and down into South America. I believe the east/west is ok.

Can someone point me to the solution? It appears to be an issue with the mapRect in my renderer but after a few hours of trying various things I have yet to solve it.

Here's the links Ive looked at so far:

MKOverlayRenderer stretches image

Proper use of MKOverlayView

MapKit Adding Raster Map via MKOverlay Weird Rendering Issue

Thank you!

method to invoke the display:

func loadRadarImage()
{
    let center = CLLocationCoordinate2D(latitude: 47.6062, longitude: -122.3320)
    let overlay = RadarOverlay(coord: center, rect: self.mapView.visibleMapRect)

    self.mapView.addOverlay(overlay, level: .aboveRoads)
}

MKOverlay:

class RadarOverlay: NSObject, MKOverlay {

    var coordinate: CLLocationCoordinate2D
    var boundingMapRect: MKMapRect

    init(coord: CLLocationCoordinate2D, rect: MKMapRect) {
        self.coordinate = coord
        self.boundingMapRect = rect
    }
}

MKOverlayRenderer:

@objcMembers
class NOAAMapOverlayRenderer : MKOverlayRenderer
{
    var radarImage : UIImage?

    init(overlay: MKOverlay, overlayImage: UIImage)
    {
        self.radarImage = overlayImage

        super.init(overlay: overlay)
    }

    override func draw(_ mapRect: MKMapRect, zoomScale: MKZoomScale, in context: CGContext)
    {
        if let mapImage = self.radarImage?.cgImage
        {
            let mapRect = rect(for: overlay.boundingMapRect) // CGRect
            context.scaleBy(x: 1.0, y: -1.0)
            context.translateBy(x: 0.0, y: CGFloat(-mapRect.size.height))
            context.draw(mapImage, in: mapRect)
        }
    }
}

Produces This:

enter image description here

Tim
  • 647
  • 1
  • 10
  • 21

1 Answers1

0

There is a typological mismatch here:

RadarOverlay(coord: center, rect: self.mapView.visibleMapRect)

You are saying that that is where the radar image should be. But how do you know? I don't see you consulting the radar image to find out what region it portrays.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • I see what you mean now that you pointed that out. How do I go from an image with an upper left (or whatever ones chooses to use) lat/lon that I want to use and an img width/height to get the region and/or mapRect? Thanks – Tim Jun 19 '20 at 17:06
  • Sorry but it seems to me I've completely answered the question based on the info you gave. I have _no_ idea what information you have about what the image signifies, i.e. where it overlaps the earth, because you have not told me. The question is why the wasn't coming out in the right place / size when superimposed on your map, and I answered that. – matt Jun 19 '20 at 17:16