views:

645

answers:

1

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;
}
+1  A: 

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

brianpeiris
Instead of using GLatLng, do you know a way to use Geocodes? Just curious..like if I did:var markers = [[624 Nord Ave, Chico CA]];geocoder = new GClientGeocoder();var point = new geocoder.getLocations(markers);or something of that sort?
tester
and thanks again*
tester
Google's API Code Playground as some great examples and source code: http://code.google.com/apis/ajax/playground/?exp=maps#map_geocoding_simple
brianpeiris
You're welcome. Happy coding!
brianpeiris