尝试加载谷歌地图时,rails 5未捕获参考错误

时间:2017-04-28 06:29:17

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

尝试使用google-maps-for-rails gem加载动态谷歌地图时,我在Chrome控制台中收到以下错误。

Uncaught ReferenceError: handler is not defined

enter image description here

  

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

  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>

不确定我在哪里出错,任何帮助都将不胜感激!

欢呼声。

1 个答案:

答案 0 :(得分: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>