【问题标题】:rails 5 Uncaught Reference Error when trying to load google map尝试加载谷歌地图时rails 5未捕获的参考错误
【发布时间】:2017-04-28 06:29:17
【问题描述】:

尝试使用 google-maps-for-rails gem 加载动态 google 地图时,我的 chrome 控制台出现以下错误。

Uncaught ReferenceError: handler is not defined

我的位置控制器索引操作(要加载地图的位置):

  def index
    @locations = Location.all

    @hash = Gmaps4rails.build_markers(@locations) do |location, marker|
      marker.lat location.latitude
      marker.lng location.longitude
    end
  end

所需的各种脚本:

<script src="https://maps.googleapis.com/maps/api/js?key=MY_KEY_HERE" async defer></script>
<script src="//cdn.rawgit.com/mahnunchik/markerclustererplus/master/dist/markerclusterer.min.js"></script>
<script>
    markers = handler.addMarkers(<%=raw @hash.to_json %>);
</script>

地图分区:

<div style='width: 800px;'>
  <div id="map" style='width: 800px; height: 400px;'></div>
</div>

不知道我在哪里出错了,任何帮助将不胜感激!

干杯。

【问题讨论】:

  • 变量handler从哪里来?那是你的错误,很清楚,变量handler 没有定义。这里没有关于 Rails 和 Ruby 的内容。

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


【解决方案1】:

你错过了handler变量:

handler = Gmaps.build('Google');

所以代码应该是:

<script src="https://maps.googleapis.com/maps/api/js?key=MY_KEY_HERE" async defer></script>
<script src="//cdn.rawgit.com/mahnunchik/markerclustererplus/master/dist/markerclusterer.min.js"></script>
<script>
    handler = Gmaps.build('Google');
    markers = handler.addMarkers(<%=raw @hash.to_json %>);
</script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-06
    • 2014-10-03
    • 1970-01-01
    • 2013-03-18
    • 2018-01-02
    • 2014-09-02
    • 2019-03-31
    • 1970-01-01
    相关资源
    最近更新 更多