Rails:使用gmaps4rails自定义信息窗口

时间:2017-03-06 20:10:48

标签: ruby-on-rails google-maps customization infowindow gmaps4rails

我一直关注github上的gmaps4rails教程,但我不了解如何自定义infowindow。我有一个收集实习数据的数据库,包括地点,名称,公司和描述。标记出现在地图上的正确位置,但是我只能在需要时显示名称或说明或公司以显示所有标记。

我是rails的新手,并不完全理解我需要更新的内容,以允许所有这些属性出现在infowindow中。我在下面附上我的代码:

实习模式:

class Internship < ApplicationRecord
    geocoded_by :address
    after_validation :geocode
end

实习观点:

<script src="//maps.google.com/maps/api/js?key=AIzaSyBj4qLUeW291Z5WvVOwzseQONRBGCnA8ds"></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> <!-- only if you need custom infoboxes -->
<div style='width: 800px;'>
   <div id="map" style='width: 1100px; height: 600px'></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();
});
</script>

实习总监:

def index
  @internships = Internship.all
  @hash = Gmaps4rails.build_markers(@internships) do |internship, marker|
  marker.lat internship.latitude
  marker.lng internship.longitude
  marker.infowindow internship.description
  end
end

非常感谢我能得到的任何帮助!

1 个答案:

答案 0 :(得分:0)

我需要向控制器添加一个数组,该数组包含数据库中的所有值,然后将数组传递给infowindow:

  def index
    @internships = Internship.all

    @internship_properties = Array.new
    @internships.each do |internship|
    @internship_properties.push(internship)
  end


 @hash = Gmaps4rails.build_markers(@internship_properties) do |internship_prop, marker|
  concat_info_window = "#{internship_prop.name}, #{internship_prop.address}, #{internship_prop.company}, #{internship_prop.title}, #{internship_prop.date}, #{internship_prop.description}"
  marker.lat internship_prop.latitude
  marker.lng internship_prop.longitude
  marker.infowindow concat_info_window

 end
end