0

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 to 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!"
  });   
    }
}
1
  • 1
    Can't quite follow the question, can you clarify? Commented May 1, 2011 at 14:39

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

How can I do it with an array ? this is the markers object: var marker = new google.maps.Marker({ position: myLatlng, map: map, title:"Hello World!" });
You can store the entire object in an array. JavaScript is dynamically typed, so you don't have to know anything about what you're storing in an array to store it there. In fact, you can store lots of different things of different types/sizes in the same array! So your code might be markers[i] = new google.maps.Marker({...}).
1

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.

Comments

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.