【问题标题】:Uncaught RangeError: Maximum call stack size exceeded - Gmap Rails未捕获的 RangeError:超出最大调用堆栈大小 - Gmap Rails
【发布时间】:2017-11-06 11:34:10
【问题描述】:

我尝试在我的主页上显示带有多个标记的 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&key=:52) 在 Ry..m.set (js?libraries=places&key=:119) 在 Sy (map.js:18) 在 Ry..m.zoom_changed (map.js:42) 在 Zc (js?libraries=places&key=:52) 在 Ry..m.set (js?libraries=places&key=:119) 在 Sy (map.js:18) 在 Ry..m.zoom_changed (map.js:42) 在 Zc (js?libraries=places&key=:52) 在 Ry._.m.set (js?libraries=places&key=:119)

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2018-01-24
      • 2014-08-01
      • 2015-10-28
      • 2013-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多