When using a tile overlay, the only method I've seen thus far is to listen to Google when it grabs tiles, take the X,Y,Z coordinates from the call, and compare that to the set of tiles.
var tileOptions = {
getTileUrl: function(coord, zoom) {
return "/tiles/" + zoom + "_" + coord.x + "_" + coord.y + ".png";
}
}
If the tile at that X,Y,Z exists, it is found and called normally. If it doesn't, there is a 404 error. In my case, I have six sets of tiles which could potentially all be active at the same time, and the script which creates the tiles ignores and does not create blank tiles, meaning that every time the map changes zoom or position, there is the possibility of having upwards of a hundred 404 errors as all the tiles attempt to load.
To try and help counter this in some part, I've limited the tile search to the region which the tiles cover:
var tileOptions = {
if (zoom>=16 && zoom<=20) {
if (zoom=16) { if (coord.x>=16000 && coord.x<=16004) { if (coord.y>=24200 && coord.y<=24203) {
getTileUrl: function(coord, zoom) {
//return the tiles
}}}
}
if (zoom=17) //...
}
if (zoom=18) //...
}
//...
}
}
However, the region spans the area of 8x10 blocks, and is not completely covered by the overlays. This method, in addition to only preventing errors when the user tries to stray away from the map, is also overly complicated and is likely slowing down the tile calling process.
Thus, I'm looking for a method of "placing" the tiles as apposed to "calling" them: instead of the map asking "do you have this tile" and risking 404 errors, a script or function would say "I have these tiles, place them" ignoring the tiles which are missing (blank), ideally only loading the tiles which are visible on the page.
Alternatively, I'm also looking for a method of having the function know what tiles exist so it only attempts to call those titles and thus avoiding 404 errors.
In short, how I can avoid 404 errors in calling tiles?
Thoughts after writing the question: I assume that the first ideal solution would be to take the stacks of if statements and convert them into a more dynamic check for the tiles and, if a tile exists, use the "return" to actually call it; however, this still seems to result in the same 404 errors but from a different function.
Using Indexes: As proposed below by meetamit (if I understand it right), create an index to check the exist/non-exist of the tiles first rather than calling the tile and using the 404 as the check. However, with a set of tiles in "Z_X_Y.png" format, how could I easily create these indexes?