gmaps4rails没有显示地图

时间:2016-03-14 07:05:43

标签: javascript ruby-on-rails gmaps4rails

在视图中

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

<script type="text/javascript">
markers = handler.addMarkers(<%=raw @hash.to_json %>);
</script>

在application.js

handler = Gmaps.build('Google');
handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){
  markers = handler.addMarkers([
    {
      "lat": 0,
      "lng": 0,
      "picture": {
        "url": "http://people.mozilla.com/~faaborg/files/shiretoko/firefoxIcon/firefox-32.png",
        "width":  32,
        "height": 32
      },
      "infowindow": "hello!"
    }
  ]);
  handler.bounds.extendWith(markers);
  handler.fitMapToBounds();
});

还包括underscore.js 在标题

<script src="//maps.google.com/maps/api/js?v=3.18&sensor=false&client=&key=&libraries=geometry&language=&hl=&region="></script> 
<script src="//google-maps-utility-library-v3.googlecode.com/svn/tags/markerclustererplus/2.0.14/src/markerclusterer_packed.js"></script>

但仍然没有显示地图..... 可以任何身体帮助我..这两个错误是控制台

ReferenceError: google is not defined
ReferenceError: handler is not defined

1 个答案:

答案 0 :(得分:0)

将js代码直接放在视图中是一件坏事。尝试以不同的方式解决这个问题 - 把你所有的&#34;映射代码&#34;在document.ready处理程序中如此

$(document).ready(function() {
    // put your code for initialising map  
});

同样在您的视图中将代码包装在处理程序中:

<script type="text/javascript">
$(document).ready(function() {
    markers = handler.addMarkers(<%=raw @hash.to_json %>);
});
</script>

应该有效