我在我的 Rails 应用程序中使用 Leafletjs,添加标记并使用图层组和叠加作为“类别”。我寻找了有关如何根据可见(“已检查”)叠加层绑定/自动放大和缩小的示例和想法,但找不到太多。
目前,我正在使用一个标记数组来存储所有地图标记并使用标记数组来绑定地图:
var group = L.featureGroup(markers);
map.fitBounds(group.getBounds());
Run Code Online (Sandbox Code Playgroud)
但我不确定如何根据地图上的可见叠加标记动态更新边界。这是我到目前为止:
var markers = [];
// a sample of the map markers
var consulting = new L.LayerGroup();
<% @maps.each do |consulting| %>
<% if consulting.category == "Consulting" %>
markers.push( L.marker( [<%= raw consulting.latitude.to_json %>, <%= raw consulting.longitude.to_json %>]));
L.marker( [<%= raw consulting.latitude.to_json %>, <%= raw consulting.longitude.to_json %>], {icon: consultingIcon} )
.bindPopup( 'hello')
.addTo(consulting);
<% end %>
<% end %>
var education = new L.LayerGroup();
<% @maps.each do |education| %>
<% …Run Code Online (Sandbox Code Playgroud)