Using IBAction Buttons to Zoom MapView

前端 未结 6 1548
谎友^
谎友^ 2021-01-02 17:17

I have an issue. My current location is displayed and centered in a map view however the map region doesn\'t get zoomed in to. I tried taking Rob\'s advice by taking span an

6条回答
  •  走了就别回头了
    2021-01-02 17:51

    OK, this is what I ended using to get my 2 IBAction buttons (zoomIn and zoomOut) to work with my mapView. When you open the app, the user location is displayed and the map zooms in and centers around it. You can then use the buttons to zoom in or zoom out by a factor of 2. *Big thanks to Rob who showed me the way.

    .h

    - (IBAction)zoomIn:(id)sender;
    
    - (IBAction)zoomOut:(id)sender;
    

    .m

    @interface LocationViewController ()
    
    @property (nonatomic) BOOL alreadySetZoomScale;
    
    @end
    
    
    
    -(void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
          fromLocation:(CLLocation *)oldLocation {
    
    
    if (!_alreadySetZoomScale)
    {
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(newLocation.coordinate,    1609, 1609); // 1 mile = 1609.34 meters
    
    self.mapView.region = region;
    [self.mapView setRegion:region animated:YES];
    _alreadySetZoomScale = YES;
    
    }
    
    else
    
    {
    [self.mapView setCenterCoordinate:newLocation.coordinate animated:YES];
    }
    
    
    - (IBAction)zoomIn:(id)sender {
    
    MKCoordinateRegion region = self.mapView.region;
    region.span.latitudeDelta /= 2.0;
    region.span.longitudeDelta /= 2.0;
    self.mapView.region = region;
    
    }
    
    - (IBAction)zoomOut:(id)sender {;
    
    MKCoordinateRegion region = self.mapView.region;
    region.span.latitudeDelta *= 2.0;
    region.span.longitudeDelta *= 2.0;
    self.mapView.region = region;
    
    }
    

    I have a couple other calls to MKMapView like self.mapView.showsUserLocation = YES; in viewDidLoad but that's pretty much it.

提交回复
热议问题