未捕获的RangeError:超出最大调用堆栈大小 - Gmap Rails

时间:2017-11-06 11:34:10

标签: javascript ruby-on-rails google-maps gmaps4rails

我尝试在我的主页上显示包含多个标记的Google地图。 这是代码:

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 %>

加载index.html页面后,脚本如下所示:

//<![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();
  });

//]]>

但是,我收到以下错误:

  

未捕获RangeError:超出最大调用堆栈大小       在Zc(js?libraries = places&amp; key =:52)       在Ry。 .m.set(js?libraries = places&amp; key =:119)       在Sy(map.js:18)       在Ry。 .m.zoom_changed(map.js:42)       在Zc(js?libraries = places&amp; key =:52)       在Ry。 .m.set(js?libraries = places&amp; key =:119)       在Sy(map.js:18)       在Ry。 .m.zoom_changed(map.js:42)       在Zc(js?libraries = places&amp; key =:52)       在Ry ._。m.set(js?libraries = places&amp; key =:119)

1 个答案:

答案 0 :(得分:0)

好吧,我用这段代码解决了它:

<% 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 %>