Search code examples
iosobjective-cgoogle-mapszoominggmsmapview

iOS: How to know zoom level of GMSMapView


Hello I want to get current zoom level of Google Map view, like in a condition to check. For example,

if(mapView.zoom==18.0)
{
 //code goes here..
}

How to get that ?


Solution

  • You can use 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
    

    Also can use MKCoordinateSpan check document for more information.

    typedef struct {
        CLLocationDegrees latitudeDelta;
        CLLocationDegrees longitudeDelta;
    } MKCoordinateSpan;