Search code examples
leaflettms

How to Calculate Tiles Coordinates in Leaflet to Request TMS Server?


In order to cache tiles for off-line use, I tried to calculate tiles coordinates according to a certain zoom level. Calculated x coordinates were correct but the y coordinates Were not.

This Old example compares actually received coordinates with that calculated. (click in the map to display results)

I was using map.project(latlng,zoom) to get the projected coordinates and then divide by tileSize which is 256. is this approach even correct ?

EDIT : Thanks to Ivan Sanchez for the orientation about y inversion in TMS. Actually after projecting the point with map.project(latlng,zoom) you need to inverse the y coordinate as follow :

  1. You calculate _globalTileRange(zoom) for the corresponding zoom level, then
  2. InvertedY = _globalTileRange(zoom).max.y - y ;

Here is another Link that shows the correct calculation of y coordinates for the current zoom of the map, for other zoom levels the globalTileRange need to be recalculated accordingly.

Regards,


Solution

  • Your approach is correct. However: