Search code examples
ruby-on-railsrubyruby-on-rails-3google-mapsgmaps4rails

How do I make an infowindow automatically display as open with Google-Maps-for-Rails


I want to display a map with the infowindow box automatically displayed for the single marker on the page, much like http://code.google.com/apis/maps/documentation/javascript/examples/map-coordinates.html

I've tried to code a callback function to accomplish this, but it's just not working for me.

<% 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, marker);
      }
    }
  </script>
<% end %>

Solution

  • Your code is almost perfect. Except that instead of:

     infowindow.open(Gmaps.map, marker);
    

    You should have:

     infowindow.open(Gmaps.map.map, marker);
    

    Indeed, Gmaps.map is a container, Gmap.map.map is the google object.

    I know these names are confusing. Sorry.

    PS: be sure to put this code under the gmaps call in your view.