Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There is a functional programming concept that can handle this well</p> <p>Basically, your function actually has three arguments, the <code>zoom</code> variable, the <code>coord</code> object, and the <code>tileNames[i]</code> value, but you want the <code>tileNames[i]</code> value to be fixed to a particular value at one time in your code, and leave the other two variables as actually variable.</p> <pre><code>function getTileUrlBuilder(tilename) { return function(coord, zoom) { return "/maps/tiles/tile" + tilename + "/" + zoom + "_" + coord.x + "_" + coord.y; }; } </code></pre> <p>Then you can use this function in your loop. However, since this is actually a common operation, you can use the <code>bind</code> method to handle this:</p> <pre><code>(function(tilename, coord, zoom) { return "/maps/tiles/tile" + tilename + "/" + zoom + "_" + coord.x + "_" + coord.y }).bind(this, tileNames[i]); </code></pre> <p>That will "bind" the implicit <code>this</code> to the current <code>this</code>, and then bind the first argument to the current value of <code>tileNames[i]</code>.</p> <p>EDIT: This, I think, should be the working code.</p> <pre><code>var tileNames = ["beloit", "parking", "access_p"]; var mapType = {}; function getTileUrlFull(tileName, coord, zoom) { return "/maps2/tiles/tiles" + tileName + "/" + zoom + "_" + coord.x + "_" + coord.y + ".png" } for(var i = 0; i &lt; tileNames.length; i++) { mapType[tileNames[i]] = new google.maps.ImageMapType({ getTileUrl: getTileUrlFull.bind(this, tileNames[i]), tileSize: new google.maps.Size(256, 256) }); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload