gpt4 book ai didi

ruby-on-rails - rails 4 : Google Maps not displaying

转载 作者:行者123 更新时间:2023-12-04 06:11:47 24 4
gpt4 key购买 nike

我正在尝试在我的 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>

然后在另一个 View 中,我有一个名为 _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 正在正确输出到页面。但是,没有显示 map 。请注意,我的 application.js 文件中有以下内容

//= require jquery
//= require jquery.turbolinks
//= require jquery_ujs
//= require jquery.ui.datepicker
//= require twitter/bootstrap
//= require turbolinks
//= require_tree .

知道为什么 map 没有显示吗?谢谢!

最佳答案

如果 div 被填满但不可见,您可能需要检查高度。要使 height: 100% 工作,它需要位于具有特定高度的容器中。要查看高度是否是问题,请尝试以下操作:

<div id="map-canvas" style="width: 400px; height: 400px"></div>

关于ruby-on-rails - rails 4 : Google Maps not displaying,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20246250/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com