views:

286

answers:

2

Hi, Ive read similar posts, but still didnt find a solution for myself. Basically I have an array with countries+towns in PHP and I need to show them on the map with markers. Here is my code:

function showAddress(markers) {

var address = "<?php echo $Fcity[$j], " , ", $Fcountry[$j]?>";
 if (geocoder) {
    geocoder.getLatLng(address, function(point) {
        if (!point) {
          alert(address + " not found");
        } else {

        var marker = new GMarker(point);
        map.addOverlay(marker);
  markers[i] = marker;  
          marker.openInfoWindowHtml(address);

        }
      }
    );
  }
}

Everything seems to work if I geocode one location, but I cant put it into loop to process all of them

for (var i = 0; i < markers.length; i++) {

     showAddress(markers[i]);
    }

Any help much appriciated

A: 

In your showAddress function, you reference markers[i].

However, you don't pass in i... that variable is not in the scope of the function. So, you aren't iterating and adding, you are adding variables over and over to a non-existent place in the array.

You either need to pass in i or not encapsulate showAddress in a function.

How about making the function showAddresses and putting the loop in the function.

Andrew Johnson
I have tried that, but it didnt help, even if put just a number in "i< "function showAddress(address) {for($i=0;$i< "<?php echo $data?>";$i++){ var address = "<?php echo $city, " , ", $country?>"; if (geocoder) { geocoder.getLatLng(address, function(point) { if (!point) { alert(address + " not found"); } else { map.addOverlay(new GMarker(point)); marker.openInfoWindowHtml(address); } } } ); } }
Vladimir
A: 

You should take a look at this official sample code, wich show how to geocode some data stored in MySQL, and adaptit to use your array.

Note: You need to use some delay (see code sample) or you'll have problems if you array contains a lot of cities.

RC
I checked this link and have done the same, but it didnt workhttp://stackoverflow.com/questions/816479/need-help-reverse-geocoding-to-append-message-to-glatlng-coordinate
Vladimir