Getting infoWindow open by default in Gmaps for rails

779 views Asked by At

This is what I have for displaying the map with markers

<%= javascript_tag do%>
var handler = Gmaps.build('Google');
handler.buildMap({ internal: {id: 'multi_markers'}}, function(){
    var markers = handler.addMarkers(<%=raw @hash.to_json %>);
    handler.bounds.extendWith(markers);
    handler.fitMapToBounds();
});
<%end%>
<% content_for :scripts do %>
  <script type="text/javascript" charset="utf-8">
    Gmaps.map.callback = function() {
      if (Gmaps.map.markers.length == 1) {
        var marker = Gmaps.map.markers[0];
        var infowindow = marker.infowindow;
         infowindow.open(Gmaps.map.map, marker);
      }
    }
  </script>
<% end %>

Not exactly sure how should I be trying to have the infoWindows/markers open default on page load.

Thanks

1

There are 1 answers

0
apneadiving On BEST ANSWER

The solution consists in triggering the click event on markers, then adjust map.

_.each(markers, function(marker){
  google.maps.event.trigger(marker.getServiceObject(), 'click');
});

Here is a working demo

And remove the part with Gmaps.map.callback, its for 1.x