## The Mercator projection

The HERE Raster Tile API serves map tiles obtained by mapping points on the surface of a sphere (the globe) to points on a plane, using the normalized Mercator projection.

The basic Mercator projection formula is this:

{λ, φ} -> x[-1, 1] y [-1, 1]

In this formula:

λ = longitude
φ = latitude
x = λ / π
y = ln(tan(π/4 + φ/2)) / π

The plane represents the globe as a square grid of map tiles. The size of the grid depends on the map zoom level. At the lowest zoom level, the world is contained in one map tile. At the next higher zoom level, the world is two tiles wide and two tiles high (2x2), at the next level above that, the grid is 4x4, then 8x8, 16x16, and so on up to the maximum zoom for a particular region. In other words, at each zoom level the tiles that make up the complete map of the world form a grid in which the number of tiles is equal to two to the power of two multiplied by the zoom level (2(2*zoom)).

The relationship between tiles at two consecutive zoom levels can be expressed as follows:

col1,z+1 = (2*colz) + 1row1,z+1 = (2*rowz) + 1

In this formula:

col = column number in the tile grid
row = row number in the tile grid
z = zoom level

The diagram below demonstrates this graphically:

Figure 1. Tiles at different zoom levels

You can use this information to obtain the grid coordinates (row and column) of the map tile for a particular geographic location in your application. The following pseudo code contains the complete algorithm:

--- javascript ---
var lat = 52.525439, // Latitude
lon = 13.38727,    // Longitude
z = 12,        // Zoom level
n,
xTile,
yTile;

latRad = lat * Math.PI / 180;
n = Math.pow(2, z);
xTile = n * ((lon + 180) / 360);

--- output ---

The zoom level and tile row and column can be used as URL variables separated by the '/' character in map tile requests. Note that they must be provided in this order: zoom/column/row. This is the [Z]/[X]/[Y] addressing scheme.