Saturday, October 13, 2018

MKMapCamera doesn't zoom to correct altitude

Leave a Comment

When I'm setting an MKMapCamera with a specific altitude on my map view (MapKit) it doesn't zoom to the correct altitude sometimes. I think it has something to do with the map not being fully loaded so it stops higher (950m or so) instead of the altitude I set (about 280m).

Initially I noticed the issue when I first loaded the map but it seems more related to the lower altitudes. Higher altitudes seems to work OK.

Here's a video demonstrating the problem: https://streamable.com/644l1 In the video I'm setting the same camera twice.

The code for setting the camera:

let distance = currentHole!.teeCoordinate.distance(to: currentHole!.greenCoordinate) let altitude = Double(distance) * 2.5  let camera = MKMapCamera(     lookingAtCenter: currentHole!.centerCoordinate(),     fromDistance: altitude,     pitch: 0.0,     heading: currentHole!.teeCoordinate.bearing(to: currentHole!.greenCoordinate) - 20 ) mapView.setCamera(camera, animated: true) 

I also tried to use something like:

UIView.animate(withDuration: 1.0, animations: { () -> Void in     self.mapView.camera = camera }, completion: { (done) -> Void in     print("Animation complete") }) 

to do the animation instead. It works better (not perfect) when setting the duration to something very high, like 10 seconds or so.

Any ideas on what might be the issue here?

UPDATE:

It seems to only happen with "Satellite Flyover" maps. Satellite is fine.

1 Answers

Answers 1

I don't know for certain why this is happening but I have a theory. When you're using the flyover map types the minimum altitude of the camera is restricted by the tallest structure in the centre of the map.

If you go to the Maps app, set it to 3D Satellite view and go directly above a tall building (say the Empire State Building in New York) you can only pinch to zoom to a little above the height of the building. If you pan the camera away from the tall structure you can pinch to zoom in further. The map won't let you zoom through or inside the structure. If you zoom in to the entrance of a tall building and try to pan towards the building, the map will adjust the altitude upwards without you pinching to zoom out to prevent you passing through the building.

So before the map is fully loaded, it doesn't know what the tallest structure at the centre is going to be. To prevent you zooming inside a tall structure, the map limits the minimum height. After the map is fully loaded and it knows that there are no tall structures it lets you zoom in closer.

When you set a long duration on the animation, it's giving the map a chance to load before it gets to the lower altitude. The map knows that there are no tall structures and allows further zooming in. I would say that if you tried a longer duration animation but throttled the network bandwidth it would stop working again.

Note that the Satellite mode allows you to pass through tall structures.

As a workaround, try using mapViewDidFinishLoadingMap: or mapViewDidFinishRenderingMap:fullyRendered: to know when to zoom in more.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment