0
votes

How leaflet does to calculate the x y coordinates of the tiles from zoom z, latitude and longitude coordinates ? what is the formula please?

Thank you

2
The way that Leaflet works can be seen by inspecting the source code, starting from L.GridLayer's _update, then L.Map's project, then L.CRS's latLngToPoint, then L.Projection.SphericalMercator's project`. But I think that you are not really interested in how Leaflet does this, but in how to calculate this yoruself, so you should start by reading wiki.openstreetmap.org/wiki/Slippy_map_tilenames instead.IvanSanchez

2 Answers

0
votes

You can use map.latlngtoContainerPoint(latlng) but when you want the formular you can look into the src

Leafet/Map.js#L1071 --> Leafet/Map.js#L1014

0
votes

In case you are referring to computation of tile z x y "coordinates" / names, then there is a very simple algorithm, as described on OpenStreetMap wiki about slippy maps: https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames

Lon./lat. to tile numbers

n = 2 ^ zoom
xtile = n * ((lon_deg + 180) / 360)
ytile = n * (1 - arsinh(tan(lat_rad)) / π)) / 2

With arsinh being the Inverse hyperbolic sine

Some background explanation:

  • X goes from 0 (left edge is 180 °W) to 2zoom − 1 (right edge is 180 °E)

  • Y goes from 0 (top edge is 85.0511 °N) to 2zoom − 1 (bottom edge is 85.0511 °S) in a Mercator projection

See the wiki page for further explanation and details, as well as some code in different programming languages.