开发者

Restore MKMapView visible state/region properly

I have a little iPad app which shows a MKMapView instance (map). In order to save the state I try saving the map.region to the NSUserDefaults. This works very well storing only the single values as doubles. On launching, the app combines the numbers and produces a valid MKCoordinateRegion. This happens in application:didFinishLaunchingWithOptions:. Now there are several problems.

1) Imagine: You leave the app in landscape orientation. The app saves the corresponding MKCoordinateRegion. The app is terminated. Now, holding the iPad landscape, launching the app, the map doesn't show the same state as before leaving the app. And no, this is not a problem of regionThatFits:. I tracked it down and this is what happens: Also when it isn't visible to the user, the app starts in portrait mode. The app restores his state. After that the map is rotated into landscape mode.

With o开发者_StackOverflow社区ther words the app saves the landscape state but restores it to portrait. If you repeat these steps the map zooms out with every launch. Not the best UX.

2) Even if problem one would be solved by restoring in the right orientation there is still the problem that the launching orientation could differ from the leaving orientation. I think one of the better ways to solve this is to store always the values for the portrait orientation also when the user leaves in landscape. By doing this the state would be restored properly because the app is launched initially in portrait by the system.

One possible way would be to change the latitude/longitude values if the app is left in landscape. But this would be too inaccurate because CLLocationDegrees differ if they are latitude or longitude.

An other way (and I think this would be the best way) would be to "simulate" a rotation of the mapview or to calculate the visible region for the portrait mode when the app is left. But I really don't know how to do this. Here is the point where I need your help and your ideas.

I appreciate every ray of hope. :) — And please tell me if I think into a wrong direction.


Hmm. If I understand your question correctly, you're saving the MKMapView's region (that is, its center and span), and would like to use these values to restore the map's center and zoom level, independently of whether the user rotates the device between sessions.

Have you tried calculating the width-to-height ratio of your map rectangle, and then multiplying out the changes to the span? That is, if your map has bounds of size 800 x 600 in landscape mode, then you'd multiply the longitudeDelta by 600/800 (0.75) and the latitudeDelta by 800/600 (1.33) to get the appropriate span for a 600 x 800 rectangle in portrait mode.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜