What I'm trying to do is import a list of addresses from an xml file and display them in a google map. When each marker is clicked, a window will appear with the address and a name given from the xml.

So far, I've gotten the code to work, with one issue..
There has to be an alert box in the for loop (see code) before I use the geocoder. With the alert box, everything shows up as it should, except that I don't need an alert box popping up everytime a marker is added. Doesn't matter what information is in the alert box, as long as it is there, it works.

But, if I take the alert box out, then all the markers will display the same name and address in its info window. The name/address displayed is the last one in my xml file.

I thought it just needed the pause so I had tried:
setTimeout("alert('');",1000);
but that didn't work, even with the alert box within the pause..

Any thoughts?

Thanks.

var map;
    var geocoder;
    var xml;
    var markers;
    var std = [];
    var address = [];
    var count = 0;

    function load() {
        map = new GMap2(document.getElementById("map"));
        map.setCenter(new GLatLng(42.663, -73.787), 11);
        map.addControl(new GLargeMapControl3D());
        build();
    }

    function build() {
        geocoder = new GClientGeocoder();
        GDownloadUrl("./student.xml", function(data) {
            xml = GXml.parse(data);

            markers = xml.documentElement.getElementsByTagName("marker");

            for (count = 0; count < markers.length; count++) {
                
                std[count] = markers[count].getAttribute("name");
                address[count] = markers[count].getAttribute("city") + ", " + markers[count].getAttribute("state");
                alert(""); //this is the alert box that makes the code work
                geocoder.getLocations(address[count], function(response) {
                    var place = response.Placemark[0];
                    var point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);

                    map.addOverlay(createMarker(point, std[count - 1], address[count - 1]));
                }); 
                
            } 
        }); 
    } 

    function createMarker(point, s, a) {
        var marker = new GMarker(point);
        GEvent.addListener(marker, "click", function() {
            marker.openInfoWindowHtml("<p style='font-size:10pt; font-family:tahoma;'>" + point + "<br>" + s + "<br>" + a);
        });
        return marker;
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.