Rails 4:Google地图无法显示

Dan*_*G2k 3 google-maps ruby-on-rails turbolinks

我正试图在我的Rails 4应用程序上显示一个简单的Google Map.我选择静态包含代码,直到我开始工作.在我的_head.html.erb部分中,我有:

<head>
  <title><%= content_for?(:title) ? yield(:title) : t('website.name') %></title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
  <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
  <%= yield(:google_maps_api) if content_for?(:google_maps_api) %>
  <%= csrf_meta_tags %>
</head>
Run Code Online (Sandbox Code Playgroud)

然后在另一个视图中,我有一个名为_google_map.html.erb的部分执行以下操作:

<% content_for(:google_maps_api) do %>
<%= javascript_include_tag "https://maps.googleapis.com/maps/api/js?key=xyz&sensor=false" %>
<script type="text/javascript">
    function initialize() {
    var mapOptions = {
        center: new google.maps.LatLng(-34.397, 150.644),
      zoom: 8
    };
    var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
  }
  google.maps.event.addDomListener(window, 'load', initialize);
</script>
<% end %>

<div id="map-canvas" style="width: 100%; height: 100%"></div>
Run Code Online (Sandbox Code Playgroud)

当我查看源代码时,js和div正在正确输出到页面.但是,不显示任何地图.请注意,我的application.js文件中包含以下内容

//= require jquery
//= require jquery.turbolinks
//= require jquery_ujs
//= require jquery.ui.datepicker
//= require twitter/bootstrap
//= require turbolinks
//= require_tree .
Run Code Online (Sandbox Code Playgroud)

知道地图没有显示的原因吗?谢谢!

csc*_*oed 10

如果它div被填满,但只是看不到你可能想要检查高度.对于height: 100%工作,它需要在具有特定高度的容器中.要查看高度是否有问题,请尝试以下方法:

<div id="map-canvas" style="width: 400px; height: 400px"></div>
Run Code Online (Sandbox Code Playgroud)