Search code examples
iosmkmapviewclgeocoder

Setting MKMapView Zoom level to a locality


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.


Solution

  • 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/