我在Active Admin中使用Gmaps4Rails(Google Maps for Rails) . 到目前为止一切都很好,除了我必须在同一个显示页面上添加多个 Map .

gem 'gmaps4rails', '~> 2.1', '>= 2.1.2'

我将脚本的导入提取为一个html,我只在show视图中调用一次,所以我没有收到错误:

/views/admin/_map_include_scripts.html.erb

<script src="//maps.google.com/maps/api/js?key=<%= ENV['GOOGLE_KEY'] %>"></script>
<script src="//cdn.rawgit.com/mahnunchik/markerclustererplus/master/dist/markerclusterer.min.js"></script>
<script src='//cdn.rawgit.com/printercu/google-maps-utility-library-v3-read-only/master/infobox/src/infobox_packed.js' type='text/javascript'></script>

然后在节目中我做:

div id: 'map' do
  render '/admin/map_include_scripts'
  markers = DeliveryMarkersService.new(delivery).orders_markers
  render '/admin/map_scripts', markers: markers, map_div_id: 'map'
end

views/admin/_map_scripts.html.erb 我有:

<script>
  handler = Gmaps.build('Google');
  handler.buildMap({ provider: {}, internal: { id: '<%= map_div_id %>' }}, function(){
    markers = handler.addMarkers(<%=raw markers.to_json %>);
    handler.bounds.extendWith(markers);
    handler.fitMapToBounds();
    handler.getMap().setZoom(15);
  });
</script>

到目前为止它工作得很棒!我看到 Map ,标记,一切 .

现在我想添加第二个div和第二个 Map so first I tried changing the first div to the following to verify that I could tell Gmaps in what div to display it

div id: 'map2' do
  render '/admin/map_include_scripts'
  markers = DeliveryMarkersService.new(delivery).orders_markers
  render '/admin/map_scripts', markers: markers, map_div_id: 'map2'
end

但是 Map 不会渲染!控制台没有错误 . 如果我发送一个无效的id(div id不存在),我确实会收到错误 .

有谁知道发生了什么?