我试图在我的Rails 4应用程序上显示一个简单的Google。现在,我选择静态地包含代码,直到我开始工作为止。在我的_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 .知道为什么地图没有显示出来吗?谢谢!
发布于 2013-11-28 00:33:08
如果div正在被填充,但只是不可见,您可能需要检查高度。要使height: 100%工作,它需要位于具有特定高度的容器中。要查看高度是否是问题所在,请尝试如下:
<div id="map-canvas" style="width: 400px; height: 400px"></div>https://stackoverflow.com/questions/20246250
复制相似问题