Uncaught RangeError: Maximum call stack size exceeded - Gmap Rails

168 views Asked by At

I try to display a Google map with multiple markers on my homepage. Here is the code:

restaurants_controller.rb

  def index
    @restaurants = Restaurant.all
    @hash = Gmaps4rails.build_markers(@restaurants) do |restaurant, marker|
     marker.lat restaurant.latitude
     marker.lng restaurant.longitude
    end
  end

index.html

<% content_for(:after_js) do %>
<%= 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 %>
<% end %>

And once the index.html page is loaded, here is what the script looks like:

//<![CDATA[

  var handler = Gmaps.build('Google');
  handler.buildMap({ internal: {id: 'multi_markers'}}, function(){
  var markers = handler.addMarkers([
    [{"lat":50.6356321,"lng":3.0256811},{"lat":50.6329377,"lng":3.0228278},{"lat":50.6332932,"lng":3.0175653},{"lat":50.632925,"lng":3.0157096},{"lat":50.6332822,"lng":3.0148667},{"lat":50.628187,"lng":3.024349},{"lat":50.6342801,"lng":3.0259863},{"lat":50.6332115,"lng":3.0169635},{"lat":50.6363798,"lng":3.0238608}],
      ]);
    handler.bounds.extendWith(markers);
    handler.fitMapToBounds();
  });

//]]>

However, I get the following error:

Uncaught RangeError: Maximum call stack size exceeded at Zc (js?libraries=places&key=:52) at Ry..m.set (js?libraries=places&key=:119) at Sy (map.js:18) at Ry..m.zoom_changed (map.js:42) at Zc (js?libraries=places&key=:52) at Ry..m.set (js?libraries=places&key=:119) at Sy (map.js:18) at Ry..m.zoom_changed (map.js:42) at Zc (js?libraries=places&key=:52) at Ry._.m.set (js?libraries=places&key=:119)

1

There are 1 answers

0
Uj Corb On

Well, I solved it using this code:

<% content_for(:after_js) do %>
        <%= javascript_tag do %>
        var handler = Gmaps.build('Google');
        handler.buildMap({ internal: { id: 'multi_markers' } }, function() {
        markers = handler.addMarkers(<%= raw @hash.to_json %>);
        handler.bounds.extendWith(markers);
        handler.fitMapToBounds();
        if (markers.length == 0) {
        handler.getMap().setZoom(2);
      } else {
      handler.getMap().setZoom(13);
    }
  });
  <% end %>
  <% end %>