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

How to use multiple maps on single page?


I want to use multipul map on single page. Here is.

# example.html.erb
<%= gmaps4rails(@json1) %>
<%= gmaps4rails(@json2) %>

Map can't display, all map white out ... Please help if anyone knows the solution.

Thanks.


Solution

  • EDIT:

    Since v1.0.0, it's really simple to add multiple maps on the same page, see wiki.


    It's not very straight as I didn't need this but it could be done quite easily.

    first map here, it triggers the load of the necessary js files.
    <%= gmaps4rails (@json1) %>
    
    /the empty divs for second map (beware to provide the proper css)
    <div id="second_container"> 
      <div id="second_map"></div>
    </div>
    
    <% content_for :scripts do %>
    <script>
    
    var counter = 0;
    
    Gmaps4Rails.callback = function() {
      if (counter == 0){
        counter += 1;
        <%=   { "map_options" => {  
                              "container_id" => "second_container", "id" => "second_map",
                              "type" => "SATELLITE", "center_longitude" => 180, "zoom" => 3, "auto_adjust" => true 
                              },
                "markers"     => { "data" => @json2 }
              }.to_gmaps4rails(true).html_safe
        %>
       }
    };
    <script>
    <% end %>