【发布时间】:2017-07-28 03:10:03
【问题描述】:
我在我的 Rails 应用程序中使用 Leafletjs,添加标记并将图层组和叠加层用作“类别”。我寻找了有关如何根据可见(“已检查”)叠加层绑定/自动放大和缩小的示例和想法,但找不到太多。
目前,我正在使用一个存储所有地图标记的标记数组,并使用标记数组来绑定地图:
var group = L.featureGroup(markers);
map.fitBounds(group.getBounds());
但我不确定如何根据地图上的可见覆盖标记动态更新边界。这是我到目前为止所拥有的:
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| %>
<% if education.category == "Education" %>
markers.push( L.marker( [<%= raw education.latitude.to_json %>, <%= raw education.longitude.to_json %>]));
L.marker( [<%= raw education.latitude.to_json %>, <%= raw education.longitude.to_json %>], {icon: educationIcon} )
.bindPopup( 'hello')
.addTo(education);
<% end %>
<% end %>
var mbAttr = '' +
'Imagery © <a href="http://mapbox.com">Mapbox</a>',
mbUrl = 'https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoidGVyZXNhc2hpaCIsImEiOiJjajU4cWFqNWgwMWgwMnFtazIycTJpbG51In0.4FH-NfH6b44wCc4BFodqWQ';
var grayscale = L.tileLayer(mbUrl, {id: 'mapbox.light', attribution: mbAttr}),
streets = L.tileLayer(mbUrl, {id: 'mapbox.streets', attribution: mbAttr});
var map = L.map('mapid', {
center: [43.6532, -79.3832],
zoom: 5,
scrollWheelZoom: false,
layers: [grayscale, consulting, education]
});
var baseLayers = {
"Grayscale": grayscale,
"Streets": streets
};
var overlays = {
"Consulting": consulting,
"Education": education
};
L.control.layers(baseLayers, overlays).addTo(map);
var group = L.featureGroup(markers);
map.fitBounds(group.getBounds());
【问题讨论】:
标签: javascript leaflet