javascript 在 Leaflet LayerGroup 中查找特定层,其中层是多边形
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34322864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Finding a specific layer in a Leaflet LayerGroup where layers are polygons
提问by AAJ
I'm trying to define a bunch of Leaflet polygons like this :
我正在尝试定义一堆这样的传单多边形:
var poly = new L.Polygon([
[10.1840229, 36.8906981],
[10.1840393, 36.8906669],
[10.1840989, 36.8906868],
[10.1840826, 36.890718],
[10.1840229, 36.8906981]
], {
'id': 'someId'
});
Then I'm grouping those polygons in a GroupLayer like so :
然后我将这些多边形分组在 GroupLayer 中,如下所示:
var group = new L.LayerGroup([poly1, poly2, ..., polyn]);
group.addTo(map);
Can I find those polygons by Id using group.getLayer() ? Or do I need to define the layers/polygons differently to be able to do this ?
我可以使用 group.getLayer() 通过 Id 找到这些多边形吗?或者我是否需要以不同的方式定义图层/多边形才能做到这一点?
回答by iH8
Leaflet assigns it own unique ID to each layer:
Leaflet 为每一层分配自己的唯一 ID:
var marker = new L.Marker(...);
console.log(marker._leaflet_id) // 24
var polygon = new L.Polygon(...);
console.log(polygon._leaflet_id) // 25
The getLayer
method of L.LayerGroup
, L.FeatureGroup
and L.GeoJSON
take those ID's as a parameter:
的getLayer
方法L.LayerGroup
,L.FeatureGroup
并将L.GeoJSON
这些 ID 作为参数:
var layerGroup = L.LayerGroup([marker, polygon]);
layerGroup.getLayer(24); // returns the marker
layerGroup.getLayer(25); // returns the polygon
You could also easily assign your own ID's:
您还可以轻松分配自己的 ID:
var marker = new L.Marker(...);
marker.id = 'foo';
var polygon = new L.Polygon(...);
polygon.id = 'bar';
And then fetch them like this:
然后像这样获取它们:
var layerGroup = L.LayerGroup([marker, polygon]);
layerGroup.eachLayer(function (layer) {
if (layer.id === 'foo') // it's the marker
if (layer.id === 'bar') // it's the polygon
});
You can easily throw that into a function and include it in L.LayerGroup
:
您可以轻松地将其放入一个函数并将其包含在L.LayerGroup
:
L.LayerGroup.include({
customGetLayer: function (id) {
for (var i in this._layers) {
if (this._layers[i].id == id) {
return this._layers[i];
}
}
}
});
var layerGroup = L.LayerGroup([marker, polygon]);
layerGroup.customGetLayer('foo'); // returns the marker
layerGroup.customGetLayer('bar'); // returns the polygon
EDIT: Didn't spot the ID in your example untill the indented edit. You can also assign it as an option like in your example and create a custom get function to retreive the layer:
编辑:直到缩进编辑才在您的示例中发现 ID。您还可以将其分配为示例中的选项,并创建一个自定义 get 函数来检索图层:
L.LayerGroup.include({
customGetLayer: function (id) {
for (var i in this._layers) {
if (this._layers[i].options.id == id) {
return this._layers[i];
}
}
}
});
var layerGroup = L.LayerGroup([marker, polygon]);
layerGroup.customGetLayer('foo'); // returns the marker
layerGroup.customGetLayer('bar'); // returns the polygon
If you ever need to identify the type of a layer you can do so by using instanceof:
如果您需要识别图层的类型,您可以使用 instanceof 来实现:
The instanceof operator tests whether an object has in its prototype chain the prototype property of a constructor.
instanceof 运算符测试对象的原型链中是否具有构造函数的原型属性。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof
var layerGroup = L.LayerGroup([marker, polygon]);
layerGroup.eachLayer(function (layer) {
if (layer instanceof L.Marker) // it's the marker
if (layer instanceof L.Polygon) // it's the polygon
});
But remember when you find yourself making common selections should ideally have put those features in separate layer/featuregroups.
但请记住,当您发现自己做出共同选择时,最好将这些特征放在单独的图层/特征组中。
回答by ghybs
The getLayer()
method of Layer Groupexpects a very specific ID: the one that is automatically assigned by Leaflet when "stamping" a layer(e.g. using myId = L.stamp(myLayer)
).
getLayer()
图层组的方法需要一个非常具体的 ID:当“标记”图层(例如使用myId = L.stamp(myLayer)
)时,Leaflet 自动分配的 ID 。
Therefore you would not be able to use pre-defined ID's.
因此,您将无法使用预定义的 ID。
If you can work with ID's defined dynamically (i.e. not known in advance), you could easily record those and use them to retrieve your layers (polygons).
如果您可以使用动态定义的 ID(即事先不知道),您可以轻松地记录这些并使用它们来检索您的图层(多边形)。