MKOverlay image get rotated on MKMapView in Swift

226 Views Asked by At

I am using MKOverlay to draw image over the 4 lat/long points

But not sure image get rotated. See board image over the MKMapView. This basket board image must be fit inside A, B, C, D co-ordinate. E is center co-ordinate.

enter image description here

here is the code

let upperleft = MKMapPoint(b.coordinate)
let lowerRight = MKMapPoint(d.coordinate)
let mapRect = MKMapRect(x: fmin(upperleft.x,lowerRight.x), y: fmin(upperleft.y,lowerRight.y), width: fabs(upperleft.x-lowerRight.x), height: fabs(upperleft.y-lowerRight.y));
let overlay = ImageOverlay(image: #imageLiteral(resourceName: "blank_court"), rect: mapRect, center: center.coordinate)
mapView.addOverlay(overlay)

Here is defined class

class ImageOverlay : NSObject, MKOverlay {
    var coordinate: CLLocationCoordinate2D
    let image:UIImage
    let boundingMapRect: MKMapRect
    
    init(image: UIImage, rect: MKMapRect, center: CLLocationCoordinate2D) {
        self.image = image
        self.boundingMapRect = rect
        self.coordinate = center
    }
}

class ImageOverlayRenderer : MKOverlayRenderer {
    
    override func draw(_ mapRect: MKMapRect, zoomScale: MKZoomScale, in context: CGContext) {
        
        guard let overlay = self.overlay as? ImageOverlay else {
            return
        }
        
        let rect = self.rect(for: overlay.boundingMapRect)
        UIGraphicsPushContext(context)
        overlay.image.draw(in: rect)
        UIGraphicsPopContext()
    }
}
0

There are 0 best solutions below