2

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,

Community
  • 1
  • 1
bydos
  • 23
  • 6

1 Answers1

1

Your approach is correct. However:

IvanSanchez
  • 18,272
  • 3
  • 30
  • 45
  • Thank you for the quick answer, the example was just to highligh the issue I am facing. I loop through the img's to get the real urls of the tiles shown on the screen. As you can see in the example the tweaked GridLayer shows the internal calculations of coords and they are not the same, not even included in the actual range (zoom in to see the big differences). – bydos Jan 13 '17 at 12:04
  • They are not the same because of the reasons I explained above. If the Y coordinate confuses you, print out `(2^zoom - y)` instead of simply `y`. – IvanSanchez Jan 13 '17 at 12:29