Search code examples
ruby-on-railsrubygoogle-mapsgmaps4rails

Get marker infowindow using gmaps4rails


I'm using Rails 4 and gem gmaps4rails. How can I get marker.infowindow in js? Here is my code:

handler = Gmaps.build('Google');
handler.buildMap({ provider: {scrollwheel: false}, internal: {id: 'event-detail-map'}}, function(){
  markers = handler.addMarkers([
      {
          "lat": "<%= @event.location.latitude %>",
          "lng": "<%= @event.location.longitude %>",
          "infowindow": 'Hello!'
      }  
  ]);
  marker = markers[0];
  marker.getServiceObject().addListener('mouseover', function () {
      marker.infowindow.open(marker.serviceObject.map,   marker.serviceObject);
  });

  handler.bounds.extendWith(markers);
  center = {lat: <%= @event.location.latitude %>, lng: <%[email protected] %>};

  handler.map.centerOn(center);
  handler.getMap().setZoom(12);
});

Console writes me this:

Uncaught TypeError: Cannot read property 'open' of undefined

Solution

  • instead of marker.infowindow.open use marker.infowindow.render_to_string refer this link (https://github.com/apneadiving/Google-Maps-for-Rails/wiki/Controller)