-1

I'm working on a google map v3. On this map I want display several markers each one with its Infowindow where shows some information about that point. The source of information is an javascript array with some data for each point, and up here everything works fine. The array contains (SOMETIMES) the address (sometimes null) and ALWAYS lat-long coordinates, so when the address isn't present I have to make a reverse-geocoding. Here my code:

var geocoder = new google.maps.Geocoder();  
for(var i=0;i<markersArray.length;i++){     
    var la=markersArray[i][0]);
    var lo=markersArray[i][1]);
    gpoint=new google.maps.LatLng( la,lo);
    var aMarker = new MarkerWithLabel({   //part of Google Maps Utility Lib
        position: gpoint,
        map: map,
        labelContent: deviceID,
        labelAnchor: new google.maps.Point(22, 0),
        labelClass: "labelStyle",
        html: "<ul><li>Speed: "+markersArray[i][3]+"</li></ul>",
        address: markersArray[i][4]
    });
    if (aMarker.address<="" || aMarker.address==null) {
        geocoder.geocode({'latLng': gpoint}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                if (results[1]) {
                    aMarker.address=results[1].formatted_address;
                }
            }
        });
    }    
    google.maps.event.addListener(aMarker, 'click', function () {
        infowindow.setContent(this.html+" "+this.address);
        infowindow.open(map, this);
    });

....
}

Almost everything is ok: the markers are in position and all them show the right infowindow with the right addresses except those where the addresses were empty and were reverse-geocoding. For this last set only the last one marker infowindow shows the right address, all other infowindow addresses's are empty. Any idea? Very thanks!

1 Answers1

0

One way to fix this is to create a function which does the reverse geocoding and can have function closure on the infowindow and the marker.

As the reverse geocode operation is asynchronous, the returned address isn't available for any except the last marker.

geocodezip
  • 158,664
  • 13
  • 220
  • 245
  • You didn't provide enough code to make it easy for me do that. Post a link to a map that works and demonstrates the problem (or a jsfiddle that does) if you want me to suggest code. – geocodezip Aug 02 '12 at 20:34