Check whether zoom level changed

后端 未结 7 1858
余生分开走
余生分开走 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 13:05

    you can listen to the mapView:regionDidChangeAnimated: method. However, this doesn't tell you if the zoom level changed, just if the map was animated.

    You will also need to listen to the region property of the map view. This contains the latitudeDelta and the longitudeDelta values which can be used to calculate if the zoom level has changed.

    i.e. in the .h file

    @class MyMapViewController {
        ...
        MKCoordinateRegion mapRegion;
        }
    @end
    

    and in your .m file

    - (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated {
        mapRegion = mapView.region;
    }
    
    - (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
        newRegion = mapView.region;
    
        if (mapRegion.span.latitudeDelta != newRegion.span.latitudeDelta ||
            mapRegion.span.longitudeDelta != newRegion.span.longitudeDelta)
            NSLog(@"The zoom has changed");
    }
    

    This should detect if the map zoom has changed.

    however, you should wach out for the zoom changing because the earth is curved :( If the map is scrolled the latitudeDelta and longitudeDelta will change slightly just because of the shape of the Earth, not because the user has zoomed. You might have to detect a large change in the deltas and ignore slight changes.

    Hope that helps.

    0 讨论(0)
提交回复
热议问题