Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8152151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:37:50+00:00 2026-06-06T15:37:50+00:00

Working with Google Maps, I have the following code: var tileNames = [base, parking,

  • 0

Working with Google Maps, I have the following code:

var tileNames = ["base", "parking", "access"];
var mapType = {};

for (var i = 0; i < tileNames.length; i++) {
    var tileOptions = {
    getTileUrl: function(coord, zoom) {
        return "/maps/tiles/tiles" + tileNames[i] + "/" + zoom + "_" + coord.x + "_" + coord.y + ".png";
    },
    tileSize: new google.maps.Size(256, 256)
    };
mapType[tileNames[i]] = new google.maps.ImageMapType(tileOptions);
};

Here is my issue: the “tileNames[i]” in the “getTileUrl” function is undefined when the function is executed. However, the “tileNames” array is only meant to be used here in defining the “mapType” object and thus, unlike the “coord” and “zoom” variables which are passed to the function, I’m looking to use “tileNames[i]” to define that part of the function. Thus, ideally, the function assigned to mapType.parking would look like this…

function(coord, zoom) {
    return "/maps/tiles/tilesparking/" + zoom + "_" + coord.x + "_" + coord.y + ".png";
};

…after the function is defined in the first piece of code. Is there any way to take the value of a variable/array and use it to statically define a function while maintaining the other two variables as variables.


Edit: Looking at the various answers below, the following is the best I’ve been able to achieve thus far. While bind might be the ideal approach in a more general scenario, in this specific case, “getTileUrl:” apparently wants a specific syntax and placing the bind around the function defined for it causes errors for Maps. In trying KGZM’s suggestion, it works in everything up to date and not in IE8 and below.

var tileNames = ["beloit", "parking", "access_p"];
var mapType = {};

for (var i = 0; i <= (tileNames.length - 1); i++) {
    (function(i) {
        tileOptions = {
            getTileUrl: function(coord, zoom) {
                return "/maps/tiles/tiles" + tileNames[i] + "/" + zoom + "_" + coord.x + "_" + coord.y + ".png";
            },
            tileSize: new google.maps.Size(256, 256)
        };
        mapType[tileNames[i]] = new google.maps.ImageMapType(tileOptions);
    })(i);
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T15:37:52+00:00Added an answer on June 6, 2026 at 3:37 pm

    There is a functional programming concept that can handle this well

    Basically, your function actually has three arguments, the zoom variable, the coord object, and the tileNames[i] value, but you want the tileNames[i] value to be fixed to a particular value at one time in your code, and leave the other two variables as actually variable.

    function getTileUrlBuilder(tilename) {
        return function(coord, zoom) {
            return "/maps/tiles/tile" + tilename + "/" + zoom + "_" + coord.x + "_" + coord.y;
        };
    }
    

    Then you can use this function in your loop. However, since this is actually a common operation, you can use the bind method to handle this:

    (function(tilename, coord, zoom) {
        return "/maps/tiles/tile" + tilename + "/" + zoom + "_" + coord.x + "_" + coord.y
    }).bind(this, tileNames[i]);
    

    That will “bind” the implicit this to the current this, and then bind the first argument to the current value of tileNames[i].

    EDIT: This, I think, should be the working 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 < tileNames.length; i++) {
        mapType[tileNames[i]] = new google.maps.ImageMapType({
            getTileUrl: getTileUrlFull.bind(this, tileNames[i]),
            tileSize: new google.maps.Size(256, 256)
        });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have my current google maps code working to the point where if I
I have a jqueryMobile app that uses google maps api and is working properly
I'm working with a Google Maps example code , this : http://www.wolfpil.de/v3/drag-from-outside.html which has
I have a v3 Google Maps working fine with dynamic markers but I am
I have been playing with Google maps I had it working but when I
I have the following code working fine, but I'm unable to add a zoom
I'm following two tutorials from Google: http://code.google.com/apis/maps/articles/phpsqlgeocode.html http://code.google.com/apis/maps/articles/phpsqlajax_v3.html#createmap I have the first tutorial working
Javascript objects can be used as maps. The following all is valid code: var
I'm working on an application that uses the Google Maps API. I have no
This is my first time working with the Google Maps API. I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.