0

I'm very new to iOS programming, and apologize fi this question sounds rather trivial. But after searching the web for hours, I've given up.

I need to get current zoom level of the map in my iOS app, so that when I change current view, I can retain zoom level. Using this code, I can get and set zoom levels. I implemented the mapView:regionDidChangeAnimated: method of MKMapViewDelegate protocol. But this method is called multiple times during initial "zoom in" animation of the map, and if during this period, I need to update the map, I might have wrong zoom level. I certainly don't want to turn off map animations. So, I was looking for a way to determine if map is currently being animated or stationary, before reading and storing zoom level.

I'm using MKMapView.

iMan Biglari
  • 4,674
  • 1
  • 38
  • 83
  • Are you using MKMapview or Google map ? – Kirtikumar A. Jul 30 '14 at 07:35
  • @kirtiavaiya `MKMapView`. – iMan Biglari Jul 30 '14 at 07:35
  • I think you may use MKMapview , can you please say why you have used MapKit – Kirtikumar A. Jul 30 '14 at 07:36
  • @kirtiavaiya My mistake. I updated the question as well. – iMan Biglari Jul 30 '14 at 07:39
  • Would you "change current view" _while_ the map is animating? That seems unlikely. Also, the MKMapView doesn't really work with "zoom level" like Google Maps does. Instead, you might find it simpler to store the region or visibleMapRect properties right before the view change and when returning, do mapView.region = savedRegion or mapView.visibleMapRect = savedVisibleMapRect. –  Jul 30 '14 at 11:16
  • @Anna I have a web service which provides my app with data every 10 seconds or so. As soon as the user opens my app, I zoom in to their current location and request data from the service. Sometimes the response comes before zoom in animation is over, and my app tries to center the map to user's location again to show points of interest. If per your suggestion I store current region and restore it later, it will stop zoom in animation. I was simply looking for a way to find out if map is stationary or still animating... – iMan Biglari Jul 31 '14 at 12:10

1 Answers1

0

You can try by below code

#define MERCATOR_RADIUS 85445659.44705395
#define MAX_GOOGLE_LEVELS 20

@interface MKMapView (ZoomLevel)
- (double)getZoomLevel;
@end

@implementation MKMapView (ZoomLevel)

- (double)getZoomLevel
{
CLLocationDegrees longitudeDelta = self.region.span.longitudeDelta;
CGFloat mapWidthInPixels = self.bounds.size.width;
double zoomScale = longitudeDelta * MERCATOR_RADIUS * M_PI / (180.0 * mapWidthInPixels);
double zoomer = MAX_GOOGLE_LEVELS - log2( zoomScale );
if ( zoomer < 0 ) zoomer = 0;
//  zoomer = round(zoomer);
return zoomer;
}

@end
Kirtikumar A.
  • 4,140
  • 43
  • 43
  • I'm actually using this code to get zoom level. But, where should I call `getZoomLevel()`? As I mentioned in my question, `mapView:regionDidChangeAnimated:` is called multiple times during initial map zoom in. – iMan Biglari Jul 30 '14 at 08:37
  • it will be call because you are trying to change the mapview , so if view is change,this method also be called – Kirtikumar A. Jul 30 '14 at 08:40
  • Let's say I change zoom level from 1 to 17. `mapView:regionDidChangeAnimated:` many times during zoom in animation. How should I know when to call `getZoomLevel()`? – iMan Biglari Jul 30 '14 at 08:48
  • you can check that event here http://stackoverflow.com/questions/8076555/mkmapview-regiondidchangeanimated-called-by-user-action-or-program – Kirtikumar A. Jul 30 '14 at 08:53
  • How should I use that code? In my case, there is no annotation for user to pick. – iMan Biglari Jul 30 '14 at 09:02