InfoWindow on Marker using MarkerClusterer

user846290 picture user846290 · Sep 28, 2011 · Viewed 7.3k times · Source

This is my html code. I've try anything to add an infowindow on the markers but it don't wanna work. My data is loading from the "Alle_Ortswahlen.page1.xml" file. Do anyone have an idea how can I add infoWindow to each marker?

<script type="text/javascript">
  google.load('maps', '3', {
    other_params: 'sensor=false'
  });
  google.setOnLoadCallback(initialize);
function initialize() {

  var stack = [];

  var center = new google.maps.LatLng(48.136, 11.586);
    var options = {
        'zoom': 5,
        'center': center,
        'mapTypeId': google.maps.MapTypeId.ROADMAP
    };
  var map = new google.maps.Map(document.getElementById("map_canvas"), options);
  GDownloadUrl("Alle_Ortswahlen.page1.xml", function(doc) {
    var xmlDoc = GXml.parse(doc);
    var markers = xmlDoc.documentElement.getElementsByTagName("ROW");
    for (var i = 0; i < markers.length; i++) {
      // obtain the attribues of each marker
      var lat = parseFloat(markers[i].getAttribute("Field4"));
      var lng = parseFloat(markers[i].getAttribute("Field6"));
      var marker = new google.maps.Marker({
            position : new google.maps.LatLng(lat, lng),
            map: map,
            title:"This is a marker"
        });     
      stack.push(marker);
    }
    var mc = new MarkerClusterer(map,stack);
  });
}
</script>

Answer

g.adam picture g.adam · May 23, 2012

Before the for cycle, make an empty infowindow object.

var infowindow = new google.maps.InfoWindow();

Than in the for cycle, after the marker, add an event listener, like this:

            google.maps.event.addListener(marker, 'click', (function(marker, i) {
                return function() {
                    infowindow.setContent("You might put some content here from your XML");
                    infowindow.open(map, marker);
                }
            })(marker, i));

There is some closure magic happening when passing the callback argument to the addListener method. If you are not familiar with it, take a look at here:

Mozilla Dev Center: Working with Closures

So, your code should look something like this:

google.load('maps', '3', {
            other_params: 'sensor=false'
        });

        google.setOnLoadCallback(initialize);

        function initialize() {

            var stack = [];

            var center = new google.maps.LatLng(48.136, 11.586);
            var options = {
                'zoom': 5,
                'center': center,
                'mapTypeId': google.maps.MapTypeId.ROADMAP
            };
            var map = new google.maps.Map(document.getElementById("map_canvas"), options);
            var infowindow = new google.maps.InfoWindow();
            GDownloadUrl("Alle_Ortswahlen.page1.xml", function(doc) {
                var xmlDoc = GXml.parse(doc);
                var markers = xmlDoc.documentElement.getElementsByTagName("ROW");
                for (var i = 0; i < markers.length; i++) {
                    // obtain the attribues of each marker
                    var lat = parseFloat(markers[i].getAttribute("Field4"));
                    var lng = parseFloat(markers[i].getAttribute("Field6"));
                    var marker = new google.maps.Marker({
                        position : new google.maps.LatLng(lat, lng),
                        map: map,
                        title:"This is a marker"
                    });     
                    google.maps.event.addListener(marker, 'click', (function(marker, i) {
                        return function() {
                            infowindow.setContent("You might put some content here from your XML");
                            infowindow.open(map, marker);
                        }
                    })(marker, i));
                    stack.push(marker);
                }
                var mc = new MarkerClusterer(map,stack);
            });
        }