Ios – How to store current MKCoordinateRegion or zoom level

iosmapkitmkcoordinateregionmkcoordinatespanmkmapview

I have an app which is providing a zoomable MKMapView to the user. I want to be able to store the user's preferred coordinates and zoom level for when the map view is first displayed.

Currently, in viewDidLoad, I am providing a default set of coordinates and zoom level for the initial map presentation:

    zoomLocation.latitude = 55.50;
    zoomLocation.longitude = -5.50;

    // specify size of region to display
    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 340.0*METERS_PER_MILE, 340.0*METERS_PER_MILE);

    // auto adjust region to fit screen
    MKCoordinateRegion adjustedRegion = [mapView regionThatFits:viewRegion];                

    // display the new region
    [mapView setRegion:adjustedRegion animated:YES];

What I am trying to do is, when the user scrolls and zooms the map to their preferred default view, they can hit the "Set Default" button and store the required properties to implemented in future whenever the view loads.

To store the coordinates of the user's selected view, I have this:

// gets coordinates of currently viewed map image
CGPoint pointCentrePoint = CGPointMake(mapView.frame.size.width/2, mapView.frame.size.height/2);
centrePoint = [mapView convertPoint:pointCentrePoint toCoordinateFromView:mapView]; 

NSLog(@"LAT: %f LON: %f", centrePoint.latitude, centrePoint.longitude);

What I'm struggling with is how to store the user's selected MKCoordinateRegion, or zoom level. Is there a way I can access this property for the current view, so it can be reused in future when the view loads?

Best Answer

Unlike the JavaScript Google Maps api, the MKMapView doesn't have a readily available "zoom level" property (it isn't really necessary for your purpose).

Work with the center and span values in the region property.

This question gives an example of how to save/load the region to NSUserDefaults.

Related Topic