Created
May 14, 2014 17:09
-
-
Save andrewljohnson/0fef9ad436d5274e2ddf to your computer and use it in GitHub Desktop.
calculate scale from TMS/Google/OSM zoom level
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
double latitude = self.mapView.mapCenter.latitude; | |
metersPerPixel *= cos(latitude * (M_PI / 180)); | |
double metersPerUnit = 1609.344; | |
int numberOfUnits = 1; | |
NSString *unitName = MILES_UNIT_STRING; | |
if (metersPerPixel < (1000/maxScaleSize)) { | |
unitName = METERS_UNIT_STRING; | |
metersPerUnit = 1; | |
} else { | |
unitName = KILOMETERS_UNIT_STRING; | |
metersPerUnit =1000; | |
} | |
double unitLength = metersPerUnit / metersPerPixel; | |
if (unitLength < maxScaleSize) { | |
numberOfUnits = pow(10, round(log10f((maxScaleSize/2.0)/unitLength))); | |
unitLength *= numberOfUnits; | |
} | |
if (unitLength < (maxScaleSize / 5)) { | |
numberOfUnits *=5; | |
} else if (unitLength < (maxScaleSize / 2)) { | |
numberOfUnits *= 2; | |
} else if (unitLength > maxScaleSize && numberOfUnits > 1) { | |
numberOfUnits/=2; | |
} | |
unitLength = metersPerUnit * numberOfUnits / metersPerPixel; | |
unitName = [NSString stringWithFormat:@"%d %@", numberOfUnits, unitName]; | |
dispatch_async(dispatch_get_main_queue(), ^{ | |
[self setScaleWithLabel:unitName andSize:unitLength]; | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment