gpt4 book ai didi

javascript - Leafletjs 动态绑定(bind) map 到可见覆盖

转载 作者:行者123 更新时间:2023-12-05 08:42:03 27 4
gpt4 key购买 nike

我在我的 Rails 应用程序中使用 leafletjs,添加标记并使用图层组和叠加层作为“类别”。我寻找有关如何根据可见(“选中”)覆盖层绑定(bind)/自动放大和缩小的示例和想法,但找不到太多。

目前,我正在使用一个存储所有 map 标记的标记数组,并使用标记数组来绑定(bind) map :

var group = L.featureGroup(markers); 
map.fitBounds(group.getBounds());

但我不确定如何根据 map 上可见的叠加标记动态更新边界。这是我到目前为止所拥有的:

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());

最佳答案

您可以使用 layeraddlayerremove 事件跟踪添加到 map 和从 map 中删除的图层。每次添加或删除一个功能组时,您都需要构建边界。带有要详细说明的评论的工作片段:

var map = new L.Map('leaflet', {
center: [0, 0],
zoom: 0,
layers: [
new L.TileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
'attribution': 'Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors'
})
]
});

map.on('layeradd layerremove', function () {
// Create new empty bounds
var bounds = new L.LatLngBounds();
// Iterate the map's layers
map.eachLayer(function (layer) {
// Check if layer is a featuregroup
if (layer instanceof L.FeatureGroup) {
// Extend bounds with group's bounds
bounds.extend(layer.getBounds());
}
});
// Check if bounds are valid (could be empty)
if (bounds.isValid()) {
// Valid, fit bounds
map.fitBounds(bounds);
} else {
// Invalid, fit world
map.fitWorld();
}
});

var markers = new L.FeatureGroup([
new L.Marker([-30, -30]),
new L.Marker([30, -30]),
new L.Marker([-30, -60]),
new L.Marker([30, -60])
]).addTo(map);

var polylines = new L.FeatureGroup([
new L.Polyline([[-30, 30], [30, 60]]),
new L.Polyline([[30, 30], [-30, 60]])
]).addTo(map);

var control = new L.Control.Layers(null, {
'Markers': markers,
'Polylines': polylines
}).addTo(map);
body {
margin: 0;
}

html, body, #leaflet {
height: 100%;
}
<!DOCTYPE html>
<html>
<head>
<title>Leaflet 1.0.3</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link type="text/css" rel="stylesheet" href="//unpkg.com/leaflet@1.0.3/dist/leaflet.css" />
</head>
<body>
<div id="leaflet"></div>
<script type="application/javascript" src="//unpkg.com/leaflet@1.0.3/dist/leaflet.js"></script>
</body>
</html>

希望对您有所帮助,祝您好运。

关于javascript - Leafletjs 动态绑定(bind) map 到可见覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45286918/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com