Skip to content

Instantly share code, notes, and snippets.

@andrewljohnson
Created May 14, 2014 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewljohnson/0fef9ad436d5274e2ddf to your computer and use it in GitHub Desktop.
Save andrewljohnson/0fef9ad436d5274e2ddf to your computer and use it in GitHub Desktop.
calculate scale from TMS/Google/OSM zoom level
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