我正在尝试在我的Rails 4应用程序上显示一个简单的谷歌地图。 目前为止,我选择静态包含代码,直到我让它工作为止。 在我的_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>
然后在另一个视图中,我有一个名为_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>
当我查看源代码时,JS和div都能够正确地输出到页面上。然而,没有地图显示。请注意,我在我的application.js文件中有以下内容。
//= require jquery
//= require jquery.turbolinks
//= require jquery_ujs
//= require jquery.ui.datepicker
//= require twitter/bootstrap
//= require turbolinks
//= require_tree .
有没有想法为什么地图没有显示? 谢谢!