3

I wish to set the zoom level (OR set region) of MKMapView such that I can show a locality. Elaborating my context with an example. I have a location (CLLocation *) of which I found out the locality using CLGeocoder (reverse geocoding). Now, say the locality is 'Cupertino, CA' area. How do I find the region that completely encloses Cupertino in MKMapView?

Thank you.

Deborshi Saha
  • 224
  • 5
  • 21
  • See http://stackoverflow.com/questions/9164920/updating-mkmapview-to-clplacemark-returned-from-clgeocoder –  Nov 03 '14 at 12:33

1 Answers1

6

Create MKCoordinateRegion object and set map view region for that:

CLLocationCoordinate2D location = CLLocationCoordinate2DMake(<LATITUDE>, <LONGITUDE>);
    MKCoordinateRegion region;
    // <LATITUDE> and <LONGITUDE> for Cupertino, CA.

    region = MKCoordinateRegionMake(location, MKCoordinateSpanMake(0.5, 0.5)); 
   // 0.5 is spanning value for region, make change if you feel to adjust bit more

    MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits:region];
    [self.mapView setRegion:adjustedRegion animated:YES];

Edit:

As you mention in your comment that you need dynamic city enclosure. In that case we need map zoom level (an integer value - default is 0) .

That means some API or web service which return's city/co-ordinates and zoom level. So that from that zoom level we can achieve map span/region calculation.

And a link to get zoom level from lat/long: http://troybrant.net/blog/2010/01/set-the-zoom-level-of-an-mkmapview/

Kampai
  • 22,848
  • 21
  • 95
  • 95
  • 1
    So the problem here is that 0.5 is a hardcoded value. As cities are of different sizes, this value should be a variable instead of random hard coded value. So I wish to know is there any better of calculating this span? – Deborshi Saha Nov 03 '14 at 10:40
  • @DeborshiSaha: Do you have any API or web service reference which returns such city zoom level? – Kampai Nov 03 '14 at 10:46
  • No. I have 'CLLocation'. Using CLGeocoder to reverse geolocate. – Deborshi Saha Nov 03 '14 at 11:05
  • @DeborshiSaha: Here is one blog, which will helps to get zoom level and according to that you can span your region. http://troybrant.net/blog/2010/01/set-the-zoom-level-of-an-mkmapview/ – Kampai Nov 03 '14 at 11:18