如何获取 Leaflet 上的所有标记
How to get all markers on Leaflet
我有一个侦听器,可以检测数据库中对象位置的变化。它将传递正在更改的对象的所有信息。
我想从当前地图中获取所有标记并找到受影响的标记。找到后,更新位置。
但是,我仍在寻找从地图上获取所有标记的最佳方法,然后我可以更新位置。
var map = L.map('map').setView([37.78541,-122.40787], 13);
var markers = new L.FeatureGroup();
var mapLink =
'<a href="http://openstreetmap.org">OpenStreetMap</a>';
L.tileLayer(
'https://{s}.tiles.mapbox.com/v4/examples.map-i87786ca/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoiZ2Vja29iIiwiYSI6IndzVjRGN0kifQ.lToORsjE0lkt-VQ2SXOb-Q', {
attribution: '© ' + mapLink + ' Contributors',
maxZoom: 18,
}).addTo(map);
var marker = createCircleMarker([44.977368, -93.232659]);
marker._id = "69"; // Id of the marker
map.addLayer(marker);
var socket = io();
socket.on('update location', function(obj) {
// Get all markers and find markers with attribute obj.name to
// update the location to [obj.lat,obj.lon]
});
在 L.map 上使用 eachLayer
方法。喜欢
map.eachLayer(function (layer) {
if (layer.options.name === 'XXXXX') {
layer.setLatLng([newLat,newLon])
}
});
文档位于 http://leafletjs.com/reference-1.2.0.html#map-eachlayer
不使用 map.eachLayer
添加选项;地图中的所有图层都在内部存储在 map._layers
.
中
使用
map._layers.forEach(function (layer) {
...
});
遍历 ALL 图层元素。不仅仅是当前可见的那些。
我有一个侦听器,可以检测数据库中对象位置的变化。它将传递正在更改的对象的所有信息。
我想从当前地图中获取所有标记并找到受影响的标记。找到后,更新位置。
但是,我仍在寻找从地图上获取所有标记的最佳方法,然后我可以更新位置。
var map = L.map('map').setView([37.78541,-122.40787], 13);
var markers = new L.FeatureGroup();
var mapLink =
'<a href="http://openstreetmap.org">OpenStreetMap</a>';
L.tileLayer(
'https://{s}.tiles.mapbox.com/v4/examples.map-i87786ca/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoiZ2Vja29iIiwiYSI6IndzVjRGN0kifQ.lToORsjE0lkt-VQ2SXOb-Q', {
attribution: '© ' + mapLink + ' Contributors',
maxZoom: 18,
}).addTo(map);
var marker = createCircleMarker([44.977368, -93.232659]);
marker._id = "69"; // Id of the marker
map.addLayer(marker);
var socket = io();
socket.on('update location', function(obj) {
// Get all markers and find markers with attribute obj.name to
// update the location to [obj.lat,obj.lon]
});
在 L.map 上使用 eachLayer
方法。喜欢
map.eachLayer(function (layer) {
if (layer.options.name === 'XXXXX') {
layer.setLatLng([newLat,newLon])
}
});
文档位于 http://leafletjs.com/reference-1.2.0.html#map-eachlayer
不使用 map.eachLayer
添加选项;地图中的所有图层都在内部存储在 map._layers
.
使用
map._layers.forEach(function (layer) {
...
});
遍历 ALL 图层元素。不仅仅是当前可见的那些。