Setting the zoom level for a MKMapView

后端 未结 15 3069
不知归路
不知归路 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:07

    Based on @AdilSoomro's great answer. I have come up with this:

    @interface MKMapView (ZoomLevel)
    - (void)setCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate
                      zoomLevel:(NSUInteger)zoomLevel
                       animated:(BOOL)animated;
    
    -(double) getZoomLevel;
    @end
    
    
    
    @implementation MKMapView (ZoomLevel)
    
    - (void)setCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate
                      zoomLevel:(NSUInteger)zoomLevel animated:(BOOL)animated {
        MKCoordinateSpan span = MKCoordinateSpanMake(0, 360/pow(2, zoomLevel)*self.frame.size.width/256);
        [self setRegion:MKCoordinateRegionMake(centerCoordinate, span) animated:animated];
    }
    
    
    -(double) getZoomLevel {
        return log2(360 * ((self.frame.size.width/256) / self.region.span.longitudeDelta));
    }
    
    @end
    

提交回复
热议问题