通过Turbolinks初始化脚本,使用Gmap4rails显示地图

时间:2016-10-21 15:04:48

标签: javascript jquery ruby-on-rails ruby-on-rails-4 turbolinks

我正在尝试使用turbolinks初始化我的脚本,请按照以下帖子: rails 4 with turbolinks and window load。  启用turbolinks后,我的地图显示效果不佳,如果我不刷新页面,我的标记是不可见的。考虑到这些事实,我尝试通过turbolinks初始化我的脚本,“page:load”,但我真的不知道怎么做,考虑我的ruby标签和我的脚本的synthax ..你知道我应该怎么做做?这是我的代码:

<script src="//maps.google.com/maps/api/js?key=[mykey]"></script>
<script src="//cdn.rawgit.com/mahnunchik/markerclustererplus/master/dist/markerclusterer.min.js"></script>
<script src='//cdn.rawgit.com/printercu/google-maps-utility-library-v3-read-only/master/infobox/src/infobox_packed.js' type='text/javascript'></script> <!-- only if you need custom infoboxes -->
<h1> Points de vente </h1>
<div style='width: 800px;'>
  <div id="map" style='width: 800px; height: 400px;'></div>
</div>
<script type="text/javascript">
handler = Gmaps.build('Google');
handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){
markers = handler.addMarkers(<%=raw @hash.to_json %>);
handler.bounds.extendWith(markers);
handler.fitMapToBounds();
});
</script>

谢谢!

1 个答案:

答案 0 :(得分:0)

我这样做了,但我的问题没有解决..我的地图在我刷新页面之前没有初始化。这是我看到的: enter image description here

&#13;
&#13;
<script src="//maps.google.com/maps/api/js?key=AIzaSyChlLKUJvuUVdSkBvg7Mf1UhdjRoMyRPjs"></script>
<script src="//cdn.rawgit.com/mahnunchik/markerclustererplus/master/dist/markerclusterer.min.js"></script>
<script src='//cdn.rawgit.com/printercu/google-maps-utility-library-v3-read-only/master/infobox/src/infobox_packed.js' type='text/javascript'></script> <!-- only if you need custom infoboxes -->
<h1> Points de vente </h1>
<div style='width: 800px;'>
  <div id="map" style='width: 800px; height: 400px;'></div>
</div>
<script type="text/javascript">
handler = Gmaps.build('Google');
handler.buildMap({ provider: {}, internal: {id: 'map'}},
ready = function(){
markers = handler.addMarkers(<%=raw @hash.to_json %>);
handler.bounds.extendWith(markers);
handler.fitMapToBounds();
google.maps.event.addDomListener(window, 'load', initialize);
});
$(document).ready(ready);
$(document).on('page:load', ready);
</script>
&#13;
&#13;
&#13;