Setting the zoom level for a MKMapView

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

    I hope following code fragment would help you.

    - (void)handleZoomOutAction:(id)sender {
        MKCoordinateRegion newRegion=MKCoordinateRegionMake(mapView.region.center,MKCoordinateSpanMake(mapView.region.s       pan.latitudeDelta/0.5, mapView.region.span.longitudeDelta/0.5));
        [mapView setRegion:newRegion];
    }
    
    
    - (void)handleZoomInAction:(id)sender {
        MKCoordinateRegion newRegion=MKCoordinateRegionMake(mapView.region.center,MKCoordinateSpanMake(mapView.region.span.latitudeDelta*0.5, mapView.region.span.longitudeDelta*0.5));
        [mapView setRegion:newRegion];
    }
    

    You can choose any value in stead of 0.5 to reduce or increase zoom level. I have used these methods on click of two buttons.

    0 讨论(0)
  • 2020-12-02 06:15

    MKMapView extension based on this answer (+ floating-point zoom level accuracy):

    import Foundation
    import MapKit
    
    extension MKMapView {
        var zoomLevel: Double {
            get {
                return log2(360 * (Double(self.frame.size.width / 256) / self.region.span.longitudeDelta)) + 1
            }
    
            set (newZoomLevel){
                setCenterCoordinate(coordinate:self.centerCoordinate, zoomLevel: newZoomLevel, animated: false)
            }
        }
    
        private func setCenterCoordinate(coordinate: CLLocationCoordinate2D, zoomLevel: Double, animated: Bool) {
            let span = MKCoordinateSpan(latitudeDelta: 0, longitudeDelta: 360 / pow(2, zoomLevel) * Double(self.frame.size.width) / 256)
            setRegion(MKCoordinateRegion(center: coordinate, span: span), animated: animated)
        }
    }
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题