javascript - 模态中的 gmaps4rails 不会加载全宽

标签 javascript ruby-on-rails ruby-on-rails-4 twitter-bootstrap-3 gmaps4rails2

在引导模式中加载 gmaps4rails 时,rails 4 和 gmaps4rails 2 有一些问题。 图像不会显示全宽,如下所示 enter image description here

如果我在浏览器上点击检查元素,它会调整 map 的大小,一切都会好起来的。 我只是不确定如何让 map 显示全宽! 谢谢

所有代码都在同一个 View 中

链接到:

<%= link_to @trip.destination, trip_path(@trip),  {:remote => true, 'data-toggle' =>  "modal", 'data-target' => '#myModal', 'data-no-turbolink' => true , 'data-type' => "html", :class => "pull-right"}%>

模态:

 <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModal"     aria-hidden="true">
    <div class="modal-dialog modal-lg">
    <div class="modal-content">
    <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;              </button>
    <h4 class="modal-title" id="myModalLabel"><%=@trip.destination%></h4>
    </div>
    <div class="modal-body">
    <div style='width: 800px;'>
    <div id="map" style='width: 800px; height: 400px;'></div>
    </div>

    </div>
    <div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
    </div>
    </div>
</div>

</div>

脚本:

<script type="text/javascript">
handler = Gmaps.build('Google');
handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){
markers = handler.addMarkers(<%=raw @hash.to_json %>);
handler.bounds.extendWith(markers);
handler.fitMapToBounds();
 handler.getMap().setZoom(12);
});
</script>

最佳答案

一旦模式显示,尝试构建谷歌地图

$('#myModal').on('shown.bs.modal', function (e) { 
 handler = Gmaps.build('Google');
 handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){
  markers = handler.addMarkers(<%=raw @hash.to_json %>);
  handler.bounds.extendWith(markers);
  handler.fitMapToBounds();
  handler.getMap().setZoom(12);
 });
});

关于javascript - 模态中的 gmaps4rails 不会加载全宽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23714991/

相关文章:

javascript - 在 laravel 中使用 ajax 显示输入中的数据

javascript - jquery点击不同的div并改变颜色并重置另一个div的颜色

ruby-on-rails - 使用 "Ruby on Rails"框架访问 Sharepoint

ruby - SHA 哈希在签署 OpenSSL 库中的文档时起什么作用?

ruby-on-rails - 通过 Devise for Rails 使用每个用户 `maximum_attempts` 值锁定用户

ruby-on-rails - 使用 EC2 + Rails + Nginx + Capistrano 重启时 Puma 重启失败

javascript - 如何知道 jQuery 是否正在后台运行动画以及如何停止它们?

javascript - Vue组件抛出错误: Cannot set property 'render' of undefined

ruby-on-rails - Postgres pg_hba.conf

javascript - 如何使用 jquery rails 获取 html 选择的下拉值?