Quadkeys
- Last UpdatedSep 17, 2024
- 2 minute read
Specifying the grid location of a map tile in terms of zoom level, column and row as described under mercator-projection.html is easy to understand and intuitive in practise. However, the grid is a two-dimensional array and as such does not offer efficient storage and retrieval. A better solution is a one-dimensional array, where each item is uniquely addressable by a single value. This is made possible by quadkeys, which combine the zoom level, column and row information for a tile in a one value.
In fact, a quadkey is a string containing a numeric value. The value is obtained by interleaving the bits of the row and column coordinates of a tile in the grid at the given zoom level, then converting the result to a base-4 number (the leading zeros are retained). The length of a quadkey string (the number of digits/characters) equals the zoom level of the tile.
For example, we can obtain the quadkey for a map tile in column 3 and row 5 at zoom level 3 as follows:
The code below shows a JavaScript implementation of the algorithm that calculates quadkeys. The inputs are the coordinates of the a map tile and the zoom level. The return value is a string containing the quadkey. The lower part of the code block shows the function called to calculate a quadkey for zoom level 16, which is also the length of the output string shown on the last line.
Note that when using a quadkey address, you cannot specify the size or
format of the map tile image. The response always includes an image that
measures 256 x 256 pixels and the format is png32
.