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 regionDidChangeAnimated:(BOOL)animated; but that's called even when the map is only dragged. Unfortunately, when the map is dragged the mapView.region.span changes as well...
Help?
10x
Much more simpler answer:
The easiest way to get an Integer of the current zoom level, is by using the MapView function: regionDidChangeAnimated. This function recognizes every change in zoom and will give you the basis for the calculation of the zoom factor.
Just insert this function into your MapView class (works for Swift 3.0):
And you will get a zoomFactor value out of it, where 0 is the most near point you can zoom into the map and every higher value is a far far away zoom... :-)
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
and in your .m file
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.
Count zoom scale in MKMapView - Swift solution
I created following extension for MKMapView, so you can get a scale of zoom on map. The solution is similar as presented above but in Swift.
There is also additional function
scaleWithPrecision(_:Int64)
for rounding that scale what allow to filter out f.ex. little zoom changes on MapViewI found this very helpful and developed the following code based on these answers.
I have the following MKMapView category in which I include a method for quickly getting the current zoom level for the map:
To obtain the zoom level, you can call the following in your delegates and determine if the zoom level has changed:
You could save a latitude delta, then when
regionDidChangeAnimated:
is called, check to see if the new latitude delta is different. I think the latitude delta stays constant as long as the map isn't zoomed.