Skip to content

Instantly share code, notes, and snippets.

@d2burke
Created July 3, 2014 15:12
Show Gist options
  • Save d2burke/ad29811b07ae31b378ff to your computer and use it in GitHub Desktop.
Save d2burke/ad29811b07ae31b378ff to your computer and use it in GitHub Desktop.
//
// MKMapView+ZoomLevel.m
//
// Created by Daniel.Burke on 7/3/14 via Nikita Galayko @ StackOverflow
// Copyright (c) 2014 Forrent.com. All rights reserved.
//
#import "MKMapView+ZoomLevel.h"
@implementation MKMapView (ZoomLevel)
- (double)zoomLevel{
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;
return zoomer;
}
@end
//
// MKMapView+ZoomLevel.h
//
// Created by Daniel.Burke on 7/3/14 via Nikita Galayko @ StackOverflow
// Copyright (c) 2014 Forrent.com. All rights reserved.
//
#import <Foundation/Foundation.h>
#define MERCATOR_RADIUS 85445659.44705395
#define MAX_GOOGLE_LEVELS 20
@import UIKit;
@interface MKMapView (ZoomLevel)
- (double)zoomLevel;
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment