0

I had a problem in placing an openInfoWindowHtml in my array of markers. Without the addListener inside the for loop, the map.addOverlay(markerArray[i]) works fine. What is wrong in my code? Thanks!

function addMarkers(){
        var tempMarker;
        var blueIcon = new GIcon(G_DEFAULT_ICON);
        blueIcon.image = "http://www...com/.../.png";

        // Set up our GMarkerOptions object
        markerOptions = { icon:blueIcon };
        for(ctr=0;ctr<default_address.length;ctr++){
            tempLatLng = new GLatLng(default_address[ctr][0], default_address[ctr][1]);
            tempMarker = new GMarker(tempLatLng,markerOptions);
            GEvent.addListener(tempMarker, "click", function()
                {tempMarker.openInfoWindowHtml("HI")});
            markerArray.push(tempMarker);
        }
            displayMarkers();

    }
    function displayMarkers(){
        map.clearOverlays();
        var i;
        for (i = 0; i < markerArray.length; i++) {
            map.addOverlay(markerArray[i]);
        }
    }

1 Answer 1

1

You have marker.openInfoWindowHtml, but what is marker, it's not defined anywhere in this function. Should that probably be markerArray[i].openInfoWindowHtml instead? I'm assuming markerArray is a global variable containing marker objects.

Update: as to the problem with all the markers opening their infowindows at the position (and with the content) of the last marker, this should work.

function addMarkers(){
    ...
            for(ctr=0;ctr<default_address.length;ctr++){
                tempLatLng = new GLatLng(default_address[ctr][0], default_address[ctr][1]);
                tempMarker = new GMarker(tempLatLng,markerOptions);

                // add an event listener for this marker
                bindInfoWindow(tempMarker, "HI");

                markerArray.push(tempMarker);
            }
}

// create a new global function for this
function bindInfoWindow(marker, html) {
     GEvent.addListener(tempMarker, "click", function() {
            marker.openInfoWindowHtml(html)
     });
} 
Sign up to request clarification or add additional context in comments.

3 Comments

im so dumb.. but i had a new problem.. when i click on the markers, the infowindows pops out of the last marker placed, not on the each marker in the map.. why is that so? i edited my code above.. thanks
It's because you're setting the event listener within a loop. I'll update my answer with some code that should fix this.
thank you so much! i am so glad that it worked well! thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.