Placing google maps markers via an Array

Hi Guys,
I’ve got a few locations that I’d like to put on a google map and can do it just fine when I use the following code:


            var map = new GMap2(document.getElementById("map_canvas"));
            map.setCenter(new GLatLng(52.583287, -0.237494), 13);
            var point1 = new GLatLng(52.583287,-0.237494)
            var point2 = new GLatLng(52.588345,-0.250711)
            map.addOverlay(new GMarker(point1));
            map.addOverlay(new GMarker(point2));

However, when I try to do it via an array the markers appear, but not in the correct location and I can’t figure out why.
Below is my version where I attempt to use an array:


    var x;
    var locations = new Array();
    locations[0] = "52.583287,-0.237494";
    locations[1] = "52.588345,-0.250711";
            for (x in locations) {
                var point = new GLatLng(locations[x]);
                map.addOverlay(new GMarker(point));
            }

I’m not quite sure which part is wrong, but if anyone can point it out, I’d really appreciate it.

Thanks

Can you show me your code after you removed the quotes?

I’ve tried that but it doesn’t work.
But you are correct that I cannot use strings, after a little research I found out that it must be a pair of float numbers.

In which case, my code should work fine when i remove the quotations, but it does not.

Any other ideas?

You are accessing the index array correctly but it returns another array so it will never work.


var point = new GLatLng(locations[x][0], locations[x][1])

That will allow you to access the correct indexes of the second array.


var x;
    var locations = new Array();
    locations[0] = [52.583287,-0.237494];
    locations[1] = [52.588345,-0.250711];

            for (x in locations) {
                var point = new GLatLng(locations[x]);
                map.addOverlay(new GMarker(point));
            }

excellent, it works perfectly.
I’ve been having a lot of trouble with that.

Thank you very much for your help.

I’m not familiar with google maps but I think I spotted your error.

You are setting the coordinates as a string and depending on how strict google maps is with strings and numeric values, it may be causing your script not to work.

Original:

new GLatLng(52.583287, -0.237494)

Your script:
var point = new GLatLng(“52.583287,-0.237494”);

You can fix this by creating a bi-dimensional array. Each index holding the x and y values separately.

Example:


var locations = [];
locations[0] = [52.583287, -0.237494];