开发者

How do I create dynamic variables in javascript ? is it possible?

I'm new to javascript and to programming itself, I'm trying to add markers in google maps api and load it's coords from mysql, I have everything done but now I got stuck into something, is it possible t开发者_StackOverflow社区o create a number of variables based on the number of coords I have ? here is what I have:

function get_values(numero, array)
{
var i;
    for(i=0;i<numero;i++)
    {
        //var i ( HERE: i want it to set variables based on i )= new google.maps.Marker({
        position: array[2], 
        //map: map, 
        //title:"Hello World!"
  });   
    }
}


It appears what you need to use is an array. This will allow you to store as many coordinates as you want and you'll be able to access them by index (number). For example, if you have 10 coordinates, they could be stored in an array like:

position[i] = array[2]

Your code looks, though, pretty broken, so I think you need more help getting started than what pointed questions on Stack Overflow will get you.


As Gordon says you need an array. If I understand correctly you want to create one marker for each iteration ?

Then I guess something like this would do the trick :

function get_values(numero, array)
{
    var i;
    var markers = new Array(numero); // create an array to store the markers
    for(i=0;i<numero;i++)
    {
        markers[i] = new google.maps.Marker({
            position: array[i], 
            map: map, 
            title: "Hello marker " + i // give a different title to each marker based on the number..
        });   
   }
   return markers;
}

This assumes that your get_values function takes the number of positions and an array of positions as parameters.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜