Setting MKMapView Zoom level to a locality

五迷三道 提交于 2020-01-04 03:46:00

问题


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.


回答1:


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/



来源:https://stackoverflow.com/questions/26712239/setting-mkmapview-zoom-level-to-a-locality

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!