Skip to content

Instantly share code, notes, and snippets.

@sergeytimoshin
Created October 19, 2012 22:41
Show Gist options
  • Save sergeytimoshin/3921145 to your computer and use it in GitHub Desktop.
Save sergeytimoshin/3921145 to your computer and use it in GitHub Desktop.
mapbox save as image
- (UIImage *) loadMapImage {
const int MINZOOM = 13;
const int MAXZOOM = 17;
// TODO: calculate these from list of cycle hire locations
const float MINLAT = 51.483145; //minimum and max lattitude in degrees
const float MAXLAT = 51.542138;
const float MINLONG = -0.2242237; //min and max longitutde in degrees
const float MAXLONG = -0.002275;
for (int zoom = MINZOOM; zoom <= MAXZOOM; zoom++){
NSLog(@"Downloading zoom level %d", zoom);
int n = pow(2.0, zoom); //n=2^ZOOM
int xMin = floor(((MINLONG + 180.0) / 360.0) * n); //longitude in degrees
int yMax = floor((1.0 - (logf(tanf(MINLAT * M_PI / 180.0) + 1.0 / cosf(MINLAT * M_PI / 180.0)) / M_PI)) / 2.0 * n); //latitude in degrees
int xMax = floor(((MAXLONG + 180.0) / 360.0) * n); //longitude in degrees
int yMin = floor((1.0 - (logf(tanf(MAXLAT * M_PI / 180.0) + 1.0 / cosf(MAXLAT * M_PI / 180.0)) / M_PI)) / 2.0 * n);
NSLog(@"n=%d, xMin=%d, xMax=%d, yMin=%d, yMax=%d",n,xMin,xMax,yMin,yMax);
int totalTiles = (xMax + 1 - xMin) * (yMax + 1 - yMin);
NSLog(@"Total tiles for this zoom level: %d", totalTiles);
for (int x = xMin; x<=xMax; x++){
// TODO: Create & drain autorelease pool for each iteration of the outer loop (we don't use the returned UIImages)
for (int y = yMin; y <= yMax; y++){
int currentTiles = (x - xMin) * (yMax + 1 - yMin) + (y - yMin + 1);
NSLog(@"Downloading zoom=%d,x=%d,y=%d (%d/%d)", zoom, x, y, currentTiles, totalTiles);
NSLog(@"progress = %f0.1", ((float) currentTiles) / totalTiles);
UIImage *tileImage = [_switchController.mapView.tileSource imageForTile:RMTileMake(x, y, zoom) inCache:[_switchController.mapView tileCache]];
return tileImage;
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment