Check whether zoom level changed

后端 未结 7 1908
余生分开走
余生分开走 2020-12-23 12:05

I\'m using MapKit on iPhone. How can I know when the user changes the zoom level (zoom in\\out the map)?

I\'ve tried to use mapView:(MKMapView *)mapView regi

7条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-23 12:57

    I found this very helpful and developed the following code based on these answers.

    - (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated 
    {
       mapRegion = self.mapView.region;
    }
    
    
    -(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
    
    MKCoordinateRegion newRegion = self.mapView.region;
    
    NSInteger zFactor;
    if ((mapRegion.span.latitudeDelta/newRegion.span.latitudeDelta) > 1.5){
        NSLog(@"Zoom in changed");
        zFactor = 20;
        CustomPlacemark *aO; 
        MKAnnotationView *aV; 
        for (aO in self.mapView.annotations) {
            aV = [[self mapView] viewForAnnotation:aO];
            aV.frame = CGRectMake(aV.frame.origin.x, aV.frame.origin.y, aV.frame.size.width+zFactor, aV.frame.size.height+zFactor);
            [[[self mapView] viewForAnnotation:aO] setFrame:aV.frame];
        }
    }
    if ((mapRegion.span.latitudeDelta/newRegion.span.latitudeDelta) < 0.75){
        NSLog(@"Zoom out");
        zFactor = -20;
        CustomPlacemark *aO; 
        MKAnnotationView *aV; 
        for (aO in self.mapView.annotations) {
            aV = [[self mapView] viewForAnnotation:aO];
            aV.frame = CGRectMake(aV.frame.origin.x, aV.frame.origin.y, aV.frame.size.width+zFactor, aV.frame.size.height+zFactor);
            [[[self mapView] viewForAnnotation:aO] setFrame:aV.frame];
        }
      }
    }
    

提交回复
热议问题