当我尝试使用 google-maps-for-rails gem 加载动态谷歌地图时,我的 Chrome 控制台中出现以下错误。
Uncaught ReferenceError: handler is not defined
My locations controller index action (where map is to be loaded):
def index
@locations = Location.all
@hash = Gmaps4rails.build_markers(@locations) do |location, marker|
marker.lat location.latitude
marker.lng location.longitude
end
end
The various scripts required:
<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>
The Map Div:
<div style='width: 800px;'>
<div id="map" style='width: 800px; height: 400px;'></div>
</div>
不知道我哪里出了问题,任何帮助将不胜感激!
干杯。
最佳答案
您错过了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>
关于javascript - 尝试加载谷歌地图时发生rails 5未捕获的引用错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43673436/