Google Maps/Places API JavaScript - Cannot read property 'setContent' of undefined
<p>I am trying to display an infoWindow on click of each marker that is placed using the places query. I am using Geolocation to find the location and then display all of the nearby pubs. I would like to be able to click each marker and it display the name of the pub (place.name).</p>

<p>The error I get in the console when clicking on the marker is "<strong>Cannot read property 'setContent' of undefined</strong>".</p>

<p>// Google Places code not including my geolocation code which works fine //</p>

<pre><code>function initMap() {
myLatLng = new google.maps.LatLng(lat,long);
var Options = {
    scrollwheel: false,
    zoom:15,
    center: myLatLng,
    disableDefaultUI: true,
    mapTypeId: google.maps.MapTypeId.ROADMAP
};

map = new google.maps.Map(document.getElementById('map'),Options);
var marker = new google.maps.Marker({
    position: myLatLng,
    map: map,
    label: "Y",
});    

      var service = new google.maps.places.PlacesService(map);
      service.nearbySearch({
        location: myLatLng, //Uses geolocation to find the following
        radius: 500,
        types: ['bar']
      }, callback);

  };

function callback(results, status) {
  if (status === google.maps.places.PlacesServiceStatus.OK) {
    for (var i = 0; i &lt; results.length; i++) {
      createMarker(results[i]);
    }
  }
}

function createMarker(place) {
  var placeLoc = place.geometry.location;
  var marker = new google.maps.Marker({
    map: map,
    position: place.geometry.location
  });

  google.maps.event.addListener(marker, 'click', function() {
    infowindow.setContent(place.name);
    infowindow.open(map, this);
  });
}
</code></pre>

<p>I have found this code that has the exact same problem as me, the infowindow doesn't load and the same error is in the console.</p>

<p><a href="https://jsfiddle.net/jimedelstein/bfdhjsyy/" rel="nofollow">https://jsfiddle.net/jimedelstein/bfdhjsyy/</a></p>

<p>and here is the error I get in the console:</p>

<p><a href="http://i.stack.imgur.com/2Pkeb.png" rel="nofollow">http://i.stack.imgur.com/2Pkeb.png</a></p>

<p>Thank you!</p>