MapView overlay is cutting off after zoom in
Asked Answered
S

1

9

I am facing a weird problem with MKMapView. I have used a MKOverlayRenderer. Now the problem is when I am zooming out image showing correctly. But in case of zoom in, some portion of the image are cutting off. It's looking like a portion of MapView is coming above the overlay. Following is my overlay renderer code.

class MapOverlayRenderer: MKOverlayRenderer {
    var overlayImage: UIImage
    var plan: Plan

    init(overlay: MKOverlay, overlayImage: UIImage, plan: Plan) {
        self.overlayImage = overlayImage
        self.plan = plan
        super.init(overlay: overlay)
    }

    override func draw(_ mapRect: MKMapRect, zoomScale: MKZoomScale, in ctx: CGContext) {
        let theMapRect = overlay.boundingMapRect
        let theRect = rect(for: theMapRect)

        // Rotate around top left corner
        ctx.rotate(by: CGFloat(degreesToRadians(plan.bearing)));

        // Draw the image
        UIGraphicsPushContext(ctx)
        overlayImage.draw(in: theRect, blendMode: CGBlendMode.normal, alpha: 1.0)
        UIGraphicsPopContext();
    }

    func degreesToRadians(_ x:Double) -> Double {
        return (M_PI * x / 180.0)
    }
}

Though I don't know the actual reason but when I am commenting ctx.rotate(by:) function this problem is been fixed. But that's not my solution cause image has to be in position.

zoom out zoom in

Shackle answered 9/2, 2017 at 13:39 Comment(4)
have you try by putting your zooming code in Dispatch Queue ?Disqualify
I am still fighting... can anyone help me??Shackle
This answer works, but needs rotation management https://mcmap.net/q/1321239/-proper-use-of-mkoverlayviewLascar
This answer works (https://mcmap.net/q/1321239/-proper-use-of-mkoverlayview), but needs rotation managementLascar
D
-1

Please Try below.

override func draw(_ mapRect: MKMapRect, zoomScale: MKZoomScale, in ctx: CGContext) {
DispatchQueue.main.async {
        let theMapRect = overlay.boundingMapRect
        let theRect = rect(for: theMapRect)
        // Rotate around top left corner
        ctx.rotate(by: CGFloat(degreesToRadians(plan.bearing)));
        // Draw the image
        UIGraphicsPushContext(ctx)
        overlayImage.draw(in: theRect, blendMode: CGBlendMode.normal, alpha: 1.0)
        UIGraphicsPopContext();
}
    }
Disqualify answered 16/2, 2017 at 7:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.