3

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?

beev
  • 1,197
  • 2
  • 16
  • 33
  • Why not just use the map view's `region` property? –  May 30 '11 at 13:12
  • Thanks, seems like the thing to do. I'm just very confused about how to use it. I'm looking at the docs and just can't make sense of it. I could really use some help in how to approach the problem. – beev May 30 '11 at 22:17
  • What part exactly is the question? How to use region itself, how to save it, how to load it? What are you planning to save to: NSUserDefaults, custom file, other? –  May 31 '11 at 01:24
  • Hi. At any given time I can access the map view's coordinates using the code above. I intend to save it to NSUserDefaults. The bit I'm struggling with is how to get a reference to the map view's current zoom level, so I can also save that to NSUserDefaults and recall it anytime. I'm not sure if I should be looking at region, or span, or both. I'm not sure how to access the value and what type of variable or pointer I will need to use. – beev May 31 '11 at 16:18
  • 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](http://stackoverflow.com/questions/1383296/why-mkmapview-region-is-different-than-requested) gives an example of how to save/load the region to NSUserDefaults. –  May 31 '11 at 17:28
  • Thanks a lot. I'll try to work it out from that other question. – beev Jun 02 '11 at 12:14
  • That's working really good - thanks so much! Do you want to answer the question formally by reposting your previous comment as an answer? Then I can accept your answer, since you have answered the question for me! – beev Jun 03 '11 at 09:42

1 Answers1

2

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.

Community
  • 1
  • 1