Search code examples
ruby-on-railsgmaps4rails

gmaps4rails single marker auto zoom


I am trying to follow the answer here to zoom out a bit (by default) when a single marker is displayed on the map. I have tried the code below, and have a working map generating, but changing setZoom has no effect. Also, I receive the following error from firebug (below code).

<%= gmaps("markers" => {"data" => @json, "options" => {"auto_zoom" => false} }) %>

<% content_for :scripts do %>
    <script type="text/javascript" charset="utf-8">
      function gmaps4rails_callback() {
        if (Gmaps4Rails.markers.length == 1) {
         //only one marker, choose the zoom level you expect
         Gmaps4Rails.map.setZoom(5);
        }
        else{
         //more than one marker, let's auto_zoom
         Gmaps4Rails.map_options.auto_zoom = true;
         Gmaps4Rails.adjust_map_to_bounds();
        }
      }
    </script>
<% end %>

The only error says:

TypeError: Yc is not a function
[Break On This Error]   

...=b}Rf[F].Fa=xk(7,Yc("f"));me[F].cb=xk(3,function(a){var b;if(b=a.ca[ec]()?j:a.ca...

Solution

  • Ok, the interface changed a lot since I posted the answer you quote.

    <%= gmaps("markers" => {"data" => @json, "options" => {"auto_zoom" => false} }) %>
    
    <% content_for :scripts do %>
        <script type="text/javascript" charset="utf-8">
          Gmaps.map.callback = function() {
            if (Gmaps.map.markers.length == 1) {
             //only one marker, choose the zoom level you expect
             setTimeout(function() { Gmaps.map.serviceObject.setZoom(5);}, 50);
            }
            else{
             //more than one marker, let's auto_zoom
             Gmaps.map.map_options.auto_zoom = true;
             Gmaps.map.adjustMapToBounds();
            }
          }
        </script>
    <% end %>