MarkerClusterer and infowindow

2,497 views
Skip to first unread message

Ross Ewing

unread,
Feb 9, 2012, 7:29:59 AM2/9/12
to google-map...@googlegroups.com
Hello all,

I'm having an issue getting a infowindow to open on a marker with the MarkerClusterer Google Map V3 API. Below is my code so far. Can anyone help?

    <script type="text/javascript">
function initialize() {
var center = new google.maps.LatLng(54.385756, -4.465256);
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 5,
center: center,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var markers = [];
for (var i = 0; i < 100; i++) {
var dataPhoto = data.posts[i];
var latLng = new google.maps.LatLng(dataPhoto.latitude,
dataPhoto.longitude);
var marker = new google.maps.Marker({
position: latLng
});
markers.push(marker);
}
var markerCluster = new MarkerClusterer(map, markers);
var contentString = '<div id="content">'+
        '<div id="siteNotice">'+
        '</div>'+
        '<h1 id="firstHeading" class="firstHeading">Uluru</h1>'+
        '<div id="bodyContent">'+
        '<p><b>Uluru</b>, also referred to as <b>Ayers Rock</b>, is a large ' +
        'sandstone rock formation in the southern part of the '+
        'Northern Territory, central Australia. It lies 335&#160;km (208&#160;mi) '+
        'south west of the nearest large town, Alice Springs; 450&#160;km '+
        '(280&#160;mi) by road. Kata Tjuta and Uluru are the two major '+
        'features of the Uluru - Kata Tjuta National Park. Uluru is '+
        'sacred to the Pitjantjatjara and Yankunytjatjara, the '+
        'Aboriginal people of the area. It has many springs, waterholes, '+
        'rock caves and ancient paintings. Uluru is listed as a World '+
        'Heritage Site.</p>'+
        '<p>Attribution: Uluru, <a href="http://en.wikipedia.org/w/index.php?title=Uluru&oldid=297882194">'+
        'http://en.wikipedia.org/w/index.php?title=Uluru</a> '+
        '(last visited June 22, 2009).</p>'+
        '</div>'+
        '</div>';
        
var infowindow = new google.maps.InfoWindow({
content: contentString
});
google.maps.event.addDomListener(marker, 'click', function() {
      infowindow.open(map,marker);
    });
}

google.maps.event.addDomListener(window, 'load', initialize);

    </script>

pogue5

unread,
Feb 9, 2012, 2:24:19 PM2/9/12
to google-map...@googlegroups.com
Are you trying to set up a click event for a single marker? If so I believe the click event you are setting up would be for the last marker object you created in the preceding for loop.

Ross Ewing

unread,
Feb 10, 2012, 11:00:21 AM2/10/12
to Google Maps JavaScript API v3
Yeah so basically at the moment, i have a loop that reads from the
json file where the data is held. I'm looking to have a click event
when the user clicks a specific marker and for that to then show the
infowindow with the details of the marker.

Any help is much appreciated.

Andrew C Leach

unread,
Feb 10, 2012, 11:07:10 AM2/10/12
to google-map...@googlegroups.com
On 10 February 2012 16:00, Ross Ewing <findstu...@googlemail.com> wrote:
> Yeah so basically at the moment, i have a loop that reads from the
> json file where the data is held. I'm looking to have a click event
> when the user clicks a specific marker and for that to then show the
> infowindow with the details of the marker.

You need a helper function to create your markers, like the
createMarker() function in Larry's example* at
http://www.geocodezip.com/v3_MW_example_map1.html

If the data is kept together with a marker in a function, closure
ensures it's kept together.

Andrew

*reworked from Mike Williams' V2 example

pogue5

unread,
Feb 10, 2012, 1:25:11 PM2/10/12
to google-map...@googlegroups.com
The given example seems good but if you have many markers you may want to use a for loop to dynamically create your markers.
Reply all
Reply to author
Forward
0 new messages