magarwal
magarwal

Reputation: 574

How to take MKMapView snapshot in landscape mode

I am able to take MKMapView snapshot in portrait mode, but not in landscape mode. I automatically rotate my UIView to landscape using the functions given below. I can see the map-view as rotated to landscape. But, when I take snapshot using MKMapSnapshotter, I get an image with map in portrait mode again.

override var shouldAutorotate: Bool {
    return true
}

override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
    return .landscapeLeft
}

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .landscapeLeft
}

My hunch is it has something to do with setting MKMapSnapshotOptions to landscape, but I am not sure how to do that.

let mapSnapshotOptions = MKMapSnapshotOptions()
mapSnapshotOptions.region = mapView.region
mapSnapshotOptions.scale = UIScreen.main.scale
mapSnapshotOptions.size = mapView.frame.size 

I expect that above code should have handled it since map-view is already rotated to landscape, but it does not.

mapView.frame.size =  (375.0, 647.0)

I observe the same value of mapView frame for both cases: portrait and landscape.

I tried using CLLocationManagerDelegate. But to no gain. The below function gets called when snapshot options are already set.

func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
    // heading is updated for delegates only after any transition has ended
    // therefore, snapshot properties are set already by the time program reaches this function
    mapView.camera.heading = newHeading.magneticHeading
    mapView.setCamera(mapView.camera, animated: true)
}

Please suggest what more can be done?

Upvotes: 1

Views: 144

Answers (0)

Related Questions