Setting the zoom level for a MKMapView

后端 未结 15 2934
不知归路
不知归路 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条回答
  •  -上瘾入骨i
    2020-12-02 06:16

    Based on the fact that longitude lines are spaced apart equally at any point of the map, there is a very simple implementation to set the centerCoordinate and zoomLevel:

    @interface MKMapView (ZoomLevel)
    
    @property (assign, nonatomic) NSUInteger zoomLevel;
    
    - (void)setCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate
                      zoomLevel:(NSUInteger)zoomLevel
                       animated:(BOOL)animated;
    
    @end
    
    
    @implementation MKMapView (ZoomLevel)
    
    - (void)setZoomLevel:(NSUInteger)zoomLevel {
        [self setCenterCoordinate:self.centerCoordinate zoomLevel:zoomLevel animated:NO];
    }
    
    - (NSUInteger)zoomLevel {
        return log2(360 * ((self.frame.size.width/256) / self.region.span.longitudeDelta)) + 1;
    }
    
    - (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];
    }
    
    @end
    

提交回复
热议问题