Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

All,

I have the following array called locations returned by a PHP ajax call to Javascript

["41.8818907,-87.6415806",
"41.8819918,-87.6416019",
"0.0,0.0",
"41.8816614,-87.6417209"]

How do I loop through these values in Javascript and create an array of markers for google maps?

Ex:

function generateMarkers(locations) {
  for (var i = 0; i < locations.length; i++) {
    new google.maps.Marker({
      position: new google.maps.LatLng(locations[i][0], locations[i][1]),
      map: map,
      title: locations[i][0]
    });
  }
}

Thanks

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

Almost. Try this:

function generateMarkers(locations) {
  for (var i = 0; i < locations.length; i++) {
    var coords = locations[i].split(",");
    new google.maps.Marker({
      position: new google.maps.LatLng(coords[0], coords[1]),
      map: map,
      title: locations[i]
    });
  }
}

The key is that you need latitude and longitude separated. And since they're coming back as comma-delimited, you just gotta split() 'em up.

share|improve this answer
 
Works Perfectly ! Thanks –  Jake Nov 9 '11 at 9:27
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.