Note that there are some explanatory texts on larger screens.

plurals
  1. POGoogle Map multiple infowindow does not work
    primarykey
    data
    text
    <p>I tried to add multiple markers and infowindow to a google map using javascript. Below is the code:</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;meta http-equiv="content-type" content="text/html; charset=UTF-8" /&gt; &lt;title&gt;Google Maps Multiple Markers&lt;/title&gt; &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"&gt;&lt;/script&gt; &lt;script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="map" style="width: 500px; height: 400px;"&gt;&lt;/div&gt; &lt;script type="text/javascript"&gt; var locations = [ '1961 East Mall Vancouver BC', '2366 Main Mall Vancouver BC', '2053 Main Mall, Vancouver, BC ' ]; var map = new google.maps.Map(document.getElementById('map'), { zoom : 14, center : new google.maps.LatLng(49.26526, -123.250541), mapTypeId : google.maps.MapTypeId.ROADMAP }); var infowindow = new google.maps.InfoWindow(); var marker, i; for (i = 0; i &lt; locations.length; i++) { var geocoder = new google.maps.Geocoder(); geocoder .geocode( { 'address' : locations[i] }, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { map.setCenter(results[0].geometry.location); marker = new google.maps.Marker({ map : map, position : results[0].geometry.location }); } else { alert('Geocode was not successful for the following reason: ' + status); } }); //add info window google.maps.event.addListener(marker, 'click', (function(marker, i) { return function() { infowindow.setContent(locations[i]); infowindow.open(map, marker); } })(marker, i)); //end of adding info window }//end of for loop &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>(Thanks to <a href="https://stackoverflow.com/questions/3059044/google-maps-js-api-v3-simple-multiple-marker-example">Google Maps JS API v3 - Simple Multiple Marker Example</a>)</p> <p>The problem is that: unless I comment </p> <pre><code> //add info window google.maps.event.addListener(marker, 'click', (function(marker, i) { return function() { infowindow.setContent(locations[i]); infowindow.open(map, marker); } })(marker, i)); //end of adding info window </code></pre> <p>I will only get one marker with no infowindow popup when clicking.</p> <p>If I comment above code block, I will get three markers, but no infowindow popup either.</p> <p>Where did I make mistake?</p> <p>Thank you.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

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