0

I'm trying to define variables within a loop. I'll drop the code here and then try and explain some more:

for (var k=0; k<nodes.length; k++){
    this[node+k] = new google.maps.Marker({
        position: new google.maps.LatLng(array1[k], array2[k]),
        map: map,
        title: node[k],
        icon: "some image file"
    });
}

I would like to create a list of variables which are to be named sing an already defined array (the array of names is called nodes in the code above). So in this loop, I would like to define a new variable "this[node+k]" to make a new google.maps.marker variable.

The purpose is to make a bunch of markers with pop-ups on a custom google map for some management software I'm trying to write.

I'm sure there must be some way to do it because I saw other code for defining variables in a loop (Which ofcourse I can no longer find... :( ).However, the names of the variables being defined in the loop were not taken from another array (as mine are).

I don't want to create var1, var2, var3. I saw how to do that. I want to create these variables using names from an array.

I apologize if the question still isn't clear but thanks for the help so far. I have a feeling it may be the google maps code confusing the situation too. So here is the original way to define the google maps marker variable:

var NAME1= new google.maps.Marker({
    position: new google.maps.LatLng(29.70600, -95.28159), // coordinates
    map: map,
    title:"NAME1", // marker title
    icon: "http://127.0.0.1/public_html/tower.gif" // icon
});

The code I have right now just repeats this code 20+ times to define all the different variables. I want to try and put this all into a for loop and define the variables using names from an array.

3
  • can you explain this better? I have no idea what you're really trying to achieve. ie. what's not working with your current code? Commented Jun 9, 2010 at 19:41
  • You want to dynamically create X number of variables? Not possible. Commented Jun 10, 2010 at 17:38
  • groups.google.com/group/google-maps-js-api-v3/browse_thread/… I thought I would add this link to anyone who is interested, this explains how to create many of these map markers inside a loop with all the information like latlong coordinates, names, etc stored in different arrays. Commented Jun 16, 2010 at 20:10

3 Answers 3

4

In your for loop you should use semicolons instead of commas:

for (var k=0; k<nodes.length; k++){
     // ...
}
Sign up to request clarification or add additional context in comments.

Comments

1

I am not sure what you are trying to do, but you might try:

var names = ["name1", "name2"];
var markers = new Array();
for (var k=0; k<names.length; k++){
    markers[k] = new google.maps.Marker({
        position: new google.maps.LatLng(array1[k], array2[k]),
        map: map,
        title: names[k],
        icon: "some image file"
    });
}

1 Comment

I believe this answer is basically the same as the one below, but I can't check both to be right :) But thanks for the help guys!
0

You can do what you describe if you create a variable outside of your for loop and then put your new creations into it. Something like this:

var maps = [];
for (var k=0; k<nodes.length; k++){
    maps[k] = new google.maps.Marker({
        position: new google.maps.LatLng(array1[k], array2[k]),
        map: map,
        title: node[k],
        icon: "some image file"
    });
}

That uses numeric keys on the maps array to keep track of the items. You can make maps an object instead and use text keys if that's better for your scenario.

1 Comment

YOU rock! Thanks so much! It feels great to compress 200 lines of code down to about 10 or 20...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.