Setting the zoom level for a MKMapView

后端 未结 15 2952
不知归路
不知归路 2020-12-02 05:46

I have a map which shows correctly, the only thing I want to do now is set the zoom level when it loads. Is there a way to do this?

Thanks

15条回答
  •  一整个雨季
    2020-12-02 06:12

    I know this is a late reply, but I've just wanted to address the issue of setting the zoom level myself. goldmine's answer is great but I found it not working sufficiently well in my application.

    On closer inspection goldmine states that "longitude lines are spaced apart equally at any point of the map". This is not true, it is in fact latitude lines that are spaced equally from -90 (south pole) to +90 (north pole). Longitude lines are spaced at their widest at the equator, converging to a point at the poles.

    The implementation I have adopted is therefore to use the latitude calculation as follows:

    @implementation MKMapView (ZoomLevel)
    
    - (void)setCenterCoordinate:(CLLocationCoordinate2D)coordinate
        zoomLevel:(NSUInteger)zoom animated:(BOOL)animated
    {
        MKCoordinateSpan span = MKCoordinateSpanMake(180 / pow(2, zoom) * 
            self.frame.size.height / 256, 0);
        [self setRegion:MKCoordinateRegionMake(coordinate, span) animated:animated];
    }
    
    @end
    

    Hope it helps at this late stage.

提交回复
热议问题