0

I'm using Google Maps API to generate a Map with a few markers, each one has a infowindow.

All of the neccessary details (lattitude, longtitude) are printed in the HTML, I used JS loop in order to get those values in order to create the markers and the infowindow.

The map generates fine but when I click on a markers I get "Uncaught TypeError: Cannot read property 'open' of undefined" when it's supposed to open an infowindow

HTML:

<select>
    <option class="markerobject" data-title="Headquarters" data-address="Suite 707, 2 Huntley St Alexandria NSW 2015" data-latitude="-33.911063" data-longitude="151.193590">Headquarters</option>
    <option class="markerobject" data-title="Melbourne" data-address="Westfield, Carindale Ground level, Kiosk 112 Carindale QLD 4152" data-latitude="-27.505068" data-longitude="153.101718">Melbourne</option>
    <option class="markerobject" data-title="Brisbane" data-address="Myer Centre Brisbane Shop 112 (Next to shaver shop) Brisbane QLD 4000" data-latitude="-27.466099" data-longitude="153.023588">Brisbane</option>
    <option class="markerobject" data-title="Carindale" data-address="Westfield, Carindale Ground level, Kiosk 112 Carindale QLD 4152" data-latitude="-27.505068" data-longitude="153.101718">Carindale</option>
</select>

Javascript:

function initialize() {

    var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
    var mapOptions = {
        zoom: 4,
        center: myLatlng
    };
    var map = new google.maps.Map(document.getElementById('allstores'), mapOptions);

    var fonekingicon = document.getElementById("mappointer");
    var fonekingiconsrc = fonekingicon.getAttribute("src");

    var markerObjects = document.getElementsByClassName("markerobject");

    var markers = [];
    var infowindows = [];

    for (var i = 0; i < markerObjects.length; ++i) {

        infowindows[i] = new google.maps.InfoWindow({
              content: markerObjects[i].getAttribute("title")
          });

        markers[i] = new google.maps.Marker({
            position: new google.maps.LatLng(
                markerObjects[i].getAttribute("data-latitude")
                ,
                markerObjects[i].getAttribute("data-longitude")         
            ),
            map: map,
            icon: fonekingiconsrc
        });

        google.maps.event.addListener(markers[i], 'click', function() {
            infowindows[i].open(map,markers[i]);
        });
    } 
}

google.maps.event.addDomListener(window, 'load', initialize);
MarkToast
  • 19
  • 2
  • 4
  • 1
    http://stackoverflow.com/questions/19586137/addeventlistener-using-for-loop-and-passing-values – easywaru Jan 06 '15 at 08:21
  • duplicate of [Google Maps JS API v3 - Simple Multiple Marker Example](http://stackoverflow.com/questions/3059044/google-maps-js-api-v3-simple-multiple-marker-example), "i" is invalid once the loop ends, it is pointing past the end of the infowindows array. – geocodezip Jan 06 '15 at 11:33

1 Answers1

4

You don't have function closure on your infowindows, see Google Maps JS API v3 - Simple Multiple Marker Example

google.maps.event.addListener(markers[i], 'click', (function(marker, i) {
  return function() {
    infowindows[i].open(map, markers[i]);
  }
})(markers[i], i));

You also have a typo in your code, there is no attribute "title" of your markerObjects, should be "data-title"

infowindows[i] = new google.maps.InfoWindow({
      content: markerObjects[i].getAttribute("data-title")
})

working fiddle

function initialize() {

  var myLatlng = new google.maps.LatLng(-25.363882, 131.044922);
  var mapOptions = {
    zoom: 4,
    center: myLatlng
  };
  var map = new google.maps.Map(document.getElementById('allstores'), mapOptions);

  // var fonekingicon = document.getElementById("mappointer");
  //var fonekingiconsrc = fonekingicon.getAttribute("src");

  var markerObjects = document.getElementsByClassName("markerobject");

  var markers = [];
  var infowindows = [];
  var bounds = new google.maps.LatLngBounds();

  for (var i = 0; i < markerObjects.length; ++i) {

    infowindows[i] = new google.maps.InfoWindow({
      content: markerObjects[i].getAttribute("data-title")
    });

    markers[i] = new google.maps.Marker({
      position: new google.maps.LatLng(
        markerObjects[i].getAttribute("data-latitude"),
        markerObjects[i].getAttribute("data-longitude")
      ),
      map: map
      // icon: fonekingiconsrc
    });
    google.maps.event.addListener(markers[i], 'click', (function(marker, i) {
      return function() {
        infowindows[i].open(map, markers[i]);
      }
    })(markers[i], i));
    bounds.extend(markers[i].getPosition());

  }
  map.fitBounds(bounds);
}

google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#allstores {
  height: 500px;
  width: 500px;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<div id="allstores" style="width:750px; height:450px; border: 2px solid #3872ac;"></div>
<select>
  <option class="markerobject" data-title="Headquarters" data-address="Suite 707, 2 Huntley St Alexandria NSW 2015" data-latitude="-33.911063" data-longitude="151.193590">Headquarters</option>
  <option class="markerobject" data-title="Melbourne" data-address="Westfield, Carindale Ground level, Kiosk 112 Carindale QLD 4152" data-latitude="-27.505068" data-longitude="153.101718">Melbourne</option>
  <option class="markerobject" data-title="Brisbane" data-address="Myer Centre Brisbane Shop 112 (Next to shaver shop) Brisbane QLD 4000" data-latitude="-27.466099" data-longitude="153.023588">Brisbane</option>
  <option class="markerobject" data-title="Carindale" data-address="Westfield, Carindale Ground level, Kiosk 112 Carindale QLD 4152" data-latitude="-27.505068" data-longitude="153.101718">Carindale</option>
</select>
geocodezip
  • 158,664
  • 13
  • 220
  • 245