iosobjective-cmkmapview

MKMapView - How to detect zoom vs drag event


How to detect zoom vs drag event on MKMapView.

I want to reload the map in case users want to drag/scroll map to new position. I don't want to reload the Map with zoom in/out event.


Solution

  • I found a solution:

    1. Keep the previous zoom level.
    2. In the regionDidChangeAnimated delegate method, get new zoom level of the Map and check it with the previous level.

    This is my code:

    #define MERCATOR_RADIUS 85445659.44705395
    #define kVerySmallValue (0.000001)
    
    - (BOOL)compare2Double:(double)first isEqualTo:(double)second {
        
        if(fabs(first - second) < kVerySmallValue)
            return YES;
        else
            return NO;
    }
                                                  
    - (double)getZoomLevel
    {
        static double maxGoogleLevels = -1.0;
        
        if (maxGoogleLevels < 0.0)
            maxGoogleLevels = log2(MKMapSizeWorld.width / 256.0);
        
        CLLocationDegrees longitudeDelta = self.mapView.region.span.longitudeDelta;
        CGFloat mapWidthInPixels = self.mapView.bounds.size.width;
        double zoomScale = longitudeDelta * MERCATOR_RADIUS * M_PI / (180.0 * mapWidthInPixels);
        double zoomer = maxGoogleLevels - log2( zoomScale );
        if ( zoomer < 0 ) zoomer = 0;
        NSLog(@"zoom: %f",zoomer);
        return zoomer;
    }
                       
    - (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
        
        double newZoom = [self getZoomLevel];
        if ([self compareDouble:newZoom isEqualTo:zoomLevel]) {
            NSLog(@"Drag");
        }else{
            zoomLevel = newZoom;
            NSLog(@"Zoom");
        }
        
    }