Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>For some reason, all the event listeners were getting the very last marker from the for loop. I think it's a JavaScript thing. <strong>You need to <a href="http://code.google.com/apis/maps/documentation/javascript/events.html#EventClosures" rel="nofollow">use closures in event listeners</a>.</strong> Like this:</p> <p><strong>update:</strong> modified code below to also open all info windows when a link is clicked by <a href="http://code.google.com/apis/maps/documentation/javascript/events.html#DomEvents" rel="nofollow">adding a DOM event listener</a>. The link must have an <code>id="link_id"</code> attribute.</p> <pre><code>function attachInfoWindow(map, marker) { var infowindow = new google.maps.InfoWindow( { content: marker.title }); google.maps.event.addListener(marker, 'click', function() { infowindow.open(map,marker); }); google.maps.event.addDomListener(document.getElementById('link_id'),'click', function() { infowindow.open(map,marker); }); } function getGoogleMap(Altitude, Latitude, Address) { //var myLatlng = new google.maps.LatLng(Altitude, Latitude, Address); var image = 'icon.png'; var myOptions = { zoom: 10, center: new google.maps.LatLng(-33.9, 151.2), mapTypeId: google.maps.MapTypeId.ROADMAP } var map = new google.maps.Map(document.getElementById("googleMap"), myOptions); var locations = [ ['Test içeriği5', -33.890542, 151.274856, 5], ['Test içeriği4', -33.923036, 151.259052, 4], ['Test içeriği3', -34.028249, 151.157507, 3], ['Test içeriği2', -33.80010128657071, 151.28747820854187, 2], ['Test içeriği1', -33.950198, 151.259302, 1] ]; for (var i = 0; i &lt; locations.length; i++) { //var image = new google.maps.MarkerImage('icon' + i + '.png', var image = new google.maps.MarkerImage('icon.png', new google.maps.Size(40, 34), new google.maps.Point(0, 0), new google.maps.Point(10, 34)); var location = locations[i]; //alert(location[3]); var myLatLng = new google.maps.LatLng(location[1], location[2]); var marker = new google.maps.Marker({ position: myLatLng, map: map, icon: image, title: location[0], zIndex: location[3] }); attachInfoWindow(map, marker); } //infowindow.open(map); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload