、私は(純粋なJavascriptの代替を作成しましたOpにjQueryが指定されていないことを考慮して)。
var pendingUrls = [];
function addPendingUrl(id, url)
{
// Add this url to our list of pending urls
pendingUrls[id].push(url);
//console.log("URL " + url + " added (" + pendingUrls[id].length + ")");
// if this is our first pending tile, signal that we just became busy
if (pendingUrls[id].length === 1) {
console.log("overlay is busy");
}
}
function addTileLoadListener(id, mapType, timeout)
{
// Initialise the sub-array for this particular id
pendingUrls[id] = [];
// Copy the original getTile function so we can override it, but still make use of the original function
mapType.baseGetTile = mapType.getTile;
// Override getTile so we may add event listeners to know when the images load
mapType.getTile = function(tileCoord, zoom, ownerDocument)
{
// Get the DOM node generated by the out-of-the-box ImageMapType
var node = mapType.baseGetTile(tileCoord, zoom, ownerDocument);
//console.log("URL " + node.firstChild.__src__ + " confirmed (" + pendingUrls[id].length + ")");
function removePendingImg(node, src, result)
{
var index = pendingUrls[id].indexOf(src);
if (index == -1)
{
//console.log("URL " + src + " " + "not found" + " (" + pendingUrls[id].length + ")");
}
else
{
pendingUrls[id].splice(index, 1);
//console.log("URL " + src + " " + result + " (" + pendingUrls[id].length + ")");
// If the pending url list is empty, emit an event to indicate that the tiles are finished loading
if (pendingUrls[id].length === 0) {
console.log("overlay is idle");
}
}
}
// Listen for any images within the node to finish loading
node.getElementsByTagName("img")[0].onload = function() {
//console.log("URL " + node.firstChild.src + " maybe loaded (" + node.firstChild.__src__ + ")");
// Check that we have loaded the final image. We detect this because the node.src ends with what is in node.__src__
var str = node.firstChild.src;
var suffix = node.firstChild.__src__;
if (str.indexOf(suffix, str.length - suffix.length) !== -1)
{
removePendingImg(node, node.firstChild.__src__, "loaded"); // Remove the image from our list of pending urls
}
};
// Limit the wait
var imgsrc = node.firstChild.__src__;
setTimeout(function() {
if (node.firstChild) // if the map has already changed and the image is not going to be loaded, the node is destroyed
{
//var index = pendingUrls[id].indexOf(node.firstChild.__src__);
//if (index != -1)
// If the image is not loaded yet (node.src changes to the same value as node.firstChild.__src__ when loaded)
var str = node.firstChild.src;
var suffix = node.firstChild.__src__;
if (!(str.indexOf(suffix, str.length - suffix.length) !== -1))
{
node.getElementsByTagName("img")[0].onload = null; // Disable the event handler for this node
removePendingImg(node, node.firstChild.__src__, "timed out"); // Remove the image from our list of pending urls
}
}
else removePendingImg(node, imgsrc, "discarded"); // Remove the image from our list of pending urls
}, timeout);
return node;
};
}
そして、これらの機能は、簡単に任意のgetTileUrl関数から呼び出すことができます。
myMapType = new google.maps.ImageMapType({
getTileUrl: function(coord, zoom)
{
var url = '//a.tile.server.com/' + zoom + '/' + coord.x + '/' + coord.y + '.png';
// Add this url to our list of pending urls, and enable the loading image if appropriate
addPendingUrl("myLayer", url);
return url;
},
tileSize: new google.maps.Size(256, 256),
opacity: 0.5
});
// Listen for all the images having been loaded
addTileLoadListener("myLayer", myMapType, 15000);
ボーナス機能:複数の層およびタイムアウトのサポート(場合、サーバが遅いかずさんです)。
すばらしいソリューションをありがとう! – mfras3r