Question

I'm stuck trying to loop through this array of GPS coordinates that puts pins on the google map.

Here is my code: http://pastie.org/466369

The problem is on line 27-36.

If I change it to the following, it will place 1 pin on that exact location, but I want it to loop through the array so I can add multiple pins:

//var markers = [];

    for (var i = 0; i < 1; i++) {
        var point = new GLatLng(39.729308,-121.854087);
        marker = new GMarker(point);
        map.addOverlay(marker);
        markers[i] = marker;
    }

Anyone know why this version below is breaking?

var markers = [
(39.729308,-121.854087),
(39.0,-121.0)
];

for (var i = 0; i < markers.length; i++) {
    var point = new GLatLng(markers[i]);
    marker = new GMarker(point);
    map.addOverlay(marker);
    markers[i] = marker;
}
Was it helpful?

Solution

First of all the following code is incorrect JavaScript.

var markers = [
(39.729308,-121.854087),
(39.0,-121.0)
];

you probably meant to do this:

var markers = [
[39.729308,-121.854087],
[39.0,-121.0]
];

Secondly, according to the documentation GLatLng takes two parameters, not an array.

Try this instead:

var point = new GLatLng(markers[i][0], markers[i][1]);

Edit

I've corrected your code and hosted it on JS Bin. It seems to be working after fixing the aforementioned issues:

http://jsbin.com/afojo

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top