Last active
January 17, 2018 03:56
-
-
Save wouterds/75dc0959b486ee9f3d48 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// MKMapView+ZoomLevel.h | |
// | |
// Created by Wouter De Schuyter on 30/11/14. | |
// Copyright (c) 2014 Wouter De Schuyter. All rights reserved. | |
// | |
#import <MapKit/MapKit.h> | |
@interface MKMapView (ZoomLevel) | |
- (double)longitudeToPixelSpaceX:(double)longitude; | |
- (double)latitudeToPixelSpaceY:(double)latitude; | |
- (double)pixelSpaceXToLongitude:(double)pixelX; | |
- (double)pixelSpaceYToLatitude:(double)pixelY; | |
- (void)setCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate zoomLevel:(NSUInteger)zoomLevel animated:(BOOL)animated; | |
- (MKCoordinateSpan)coordinateSpanWithMapView:(MKMapView *)mapView centerCoordinate:(CLLocationCoordinate2D)centerCoordinate andZoomLevel:(NSUInteger)zoomLevel; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// MKMapView+ZoomLevel.m | |
// | |
// Created by Wouter De Schuyter on 30/11/14. | |
// Copyright (c) 2014 Wouter De Schuyter. All rights reserved. | |
// | |
#import "MKMapView+ZoomLevel.h" | |
#define MERCATOR_OFFSET 268435456 | |
#define MERCATOR_RADIUS 85445659.44705395 | |
@implementation MKMapView (ZoomLevel) | |
- (double)longitudeToPixelSpaceX:(double)longitude | |
{ | |
return round(MERCATOR_OFFSET + MERCATOR_RADIUS * longitude * M_PI / 180.0); | |
} | |
- (double)latitudeToPixelSpaceY:(double)latitude | |
{ | |
return round(MERCATOR_OFFSET - MERCATOR_RADIUS * logf((1 + sinf(latitude * M_PI / 180.0)) / (1 - sinf(latitude * M_PI / 180.0))) / 2.0); | |
} | |
- (double)pixelSpaceXToLongitude:(double)pixelX | |
{ | |
return ((round(pixelX) - MERCATOR_OFFSET) / MERCATOR_RADIUS) * 180.0 / M_PI; | |
} | |
- (double)pixelSpaceYToLatitude:(double)pixelY | |
{ | |
return (M_PI / 2.0 - 2.0 * atan(exp((round(pixelY) - MERCATOR_OFFSET) / MERCATOR_RADIUS))) * 180.0 / M_PI; | |
} | |
- (void)setCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate zoomLevel:(NSUInteger)zoomLevel animated:(BOOL)animated | |
{ | |
zoomLevel = MIN(zoomLevel, 28); | |
MKCoordinateSpan span = [self coordinateSpanWithMapView:self centerCoordinate:centerCoordinate andZoomLevel:zoomLevel]; | |
MKCoordinateRegion region = MKCoordinateRegionMake(centerCoordinate, span); | |
[self setRegion:region animated:animated]; | |
} | |
- (MKCoordinateSpan)coordinateSpanWithMapView:(MKMapView *)mapView centerCoordinate:(CLLocationCoordinate2D)centerCoordinate andZoomLevel:(NSUInteger)zoomLevel | |
{ | |
double centerPixelX = [self longitudeToPixelSpaceX:centerCoordinate.longitude]; | |
double centerPixelY = [self latitudeToPixelSpaceY:centerCoordinate.latitude]; | |
NSInteger zoomExponent = 20 - zoomLevel; | |
double zoomScale = pow(2, zoomExponent); | |
CGSize mapSizeInPixels = mapView.bounds.size; | |
double scaledMapWidth = mapSizeInPixels.width * zoomScale; | |
double scaledMapHeight = mapSizeInPixels.height * zoomScale; | |
double topLeftPixelX = centerPixelX - (scaledMapWidth / 2); | |
double topLeftPixelY = centerPixelY - (scaledMapHeight / 2); | |
CLLocationDegrees minLng = [self pixelSpaceXToLongitude:topLeftPixelX]; | |
CLLocationDegrees maxLng = [self pixelSpaceXToLongitude:topLeftPixelX + scaledMapWidth]; | |
CLLocationDegrees longitudeDelta = maxLng - minLng; | |
CLLocationDegrees minLat = [self pixelSpaceYToLatitude:topLeftPixelY]; | |
CLLocationDegrees maxLat = [self pixelSpaceYToLatitude:topLeftPixelY + scaledMapHeight]; | |
CLLocationDegrees latitudeDelta = -1 * (maxLat - minLat); | |
MKCoordinateSpan span = MKCoordinateSpanMake(latitudeDelta, longitudeDelta); | |
return span; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment