Setting the zoom level for a MKMapView

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

    Based on quentinadam's answer

    Swift 5.1

    // size refers to the width/height of your tile images, by default is 256.0
    // Seems to get better results using round()
    // frame.width is the width of the MKMapView
    
    let zoom = round(log2(360 * Double(frame.width) / size / region.span.longitudeDelta))
    
    0 讨论(0)
  • 2020-12-02 06:05

    You can also zoom by using MKCoordinateRegion and setting its span latitude & longitude delta. Below is a quick reference and here is the iOS reference. It won't do anything fancy but should allow you to set zoom when it draws the map.


    MKCoordinateRegion region;
    region.center.latitude = {desired lat};
    region.center.longitude = {desired lng};
    region.span.latitudeDelta = 1;
    region.span.longitudeDelta = 1;
    mapView.region = region;
    

    Edit 1:

    MKCoordinateRegion region;
    region.center.latitude = {desired lat};
    region.center.longitude = {desired lng};
    region.span.latitudeDelta = 1;
    region.span.longitudeDelta = 1;
    region = [mapView regionThatFits:region];
    [mapView setRegion:region animated:TRUE];
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-02 06:11

    Swift:

    Map.setRegion(MKCoordinateRegion(center: locValue, latitudinalMeters: 200, longitudinalMeters: 200), animated: true)
    

    locValue is your coordinate.

    0 讨论(0)
  • 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.

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

    I found myself a solution, which is very simple and does the trick. Use MKCoordinateRegionMakeWithDistance in order to set the distance in meters vertically and horizontally to get the desired zoom. And then of course when you update your location you'll get the right coordinates, or you can specify it directly in the CLLocationCoordinate2D at startup, if that's what you need to do:

    CLLocationCoordinate2D noLocation;
    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(noLocation, 500, 500);
    MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits:viewRegion];          
    [self.mapView setRegion:adjustedRegion animated:YES];
    self.mapView.showsUserLocation = YES;
    

    Swift:

    let location = ...
    let region = MKCoordinateRegion( center: location.coordinate, latitudinalMeters: CLLocationDistance(exactly: 5000)!, longitudinalMeters: CLLocationDistance(exactly: 5000)!)
    mapView.setRegion(mapView.regionThatFits(region), animated: true)
    
    0 讨论(0)
提交回复
热议问题