Skip to content

Instantly share code, notes, and snippets.

@patrickyin
Created August 26, 2017 12:02
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 patrickyin/9bdbb0d1a419fbc0cf029300e9f1af21 to your computer and use it in GitHub Desktop.
Save patrickyin/9bdbb0d1a419fbc0cf029300e9f1af21 to your computer and use it in GitHub Desktop.
TilePosition
public TilePosition getTilePosition(float zoomLevel, double latitude, double longitude) {
int tileZoom = round(zoomLevel);
int tileX = (int) ((longitude + 180.0) / 360.0 * pow(2.0, tileZoom));
int tileY = (int)((1.0 - log(tan(latitude * PI / 180.0) + 1.0 / cos(latitude * PI / 180.0)) / PI) / 2.0 * pow(2.0, tileZoom));
tileY = (int) (pow(2.0, tileZoom) - tileY - 1.0); // flip Y, this is only for mbtiles file
return new TilePosition(tileZoom, tileX, tileY);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment