Leaflet 在Mapbox中访问featuregroup中图层的geojson属性

Leaflet 在Mapbox中访问featuregroup中图层的geojson属性,leaflet,mapbox,geojson,Leaflet,Mapbox,Geojson,我有一个GEOJSON,我将它添加到我的Mapbox地图中的featuregroup中,如下所示: var featureCollection = { "type": "FeatureCollection", "features": [{ "type": "Feature", "properties": { "id": 1 }, "geometry": { "type": "Point", "coordinates":

我有一个GEOJSON,我将它添加到我的Mapbox地图中的featuregroup中,如下所示:

var featureCollection = {
  "type": "FeatureCollection",
  "features": [{
    "type": "Feature",
    "properties": {
      "id": 1
    },
    "geometry": {
      "type": "Point",
      "coordinates": [0, 0]
    }
  },{
    "type": "Feature",
    "properties": {
      "id": 2
    },
    "geometry": {
      "type": "Point",
      "coordinates": [30, 30]
    }
  },{
    "type": "Feature",
    "properties": {
      "id": 3
    },
    "geometry": {
      "type": "Point",
      "coordinates": [-30, -30]
    }
  }]
};
var geojson = L.geoJson(featureCollection);
var featureGroup = L.featureGroup().addTo(map);
featureGroup.addLayer(geojson);
var featureLayer = L.mapbox.featureLayer(featureCollection).addTo(map);
featureLayer.eachLayer(function (layer) {
  layer.on('click', function (e) {
    console.log('Clicked feature ID: ' + e.target.feature.properties.id);
  });
});
featureGroup.eachLayer(function (layer) {
  var id = layer.feature.properties.id;
  testfunction(id);
});
现在,我希望在遍历featuregroup时访问每个层的id属性,以便将其作为参数传递给另一个函数。对于featurelayer,我可以通过以下方式轻松访问它:

var featureCollection = {
  "type": "FeatureCollection",
  "features": [{
    "type": "Feature",
    "properties": {
      "id": 1
    },
    "geometry": {
      "type": "Point",
      "coordinates": [0, 0]
    }
  },{
    "type": "Feature",
    "properties": {
      "id": 2
    },
    "geometry": {
      "type": "Point",
      "coordinates": [30, 30]
    }
  },{
    "type": "Feature",
    "properties": {
      "id": 3
    },
    "geometry": {
      "type": "Point",
      "coordinates": [-30, -30]
    }
  }]
};
var geojson = L.geoJson(featureCollection);
var featureGroup = L.featureGroup().addTo(map);
featureGroup.addLayer(geojson);
var featureLayer = L.mapbox.featureLayer(featureCollection).addTo(map);
featureLayer.eachLayer(function (layer) {
  layer.on('click', function (e) {
    console.log('Clicked feature ID: ' + e.target.feature.properties.id);
  });
});
featureGroup.eachLayer(function (layer) {
  var id = layer.feature.properties.id;
  testfunction(id);
});
但我希望能够在featuregroup内部循环时访问它,并且我希望能够在不进行“单击”或任何此类事件的情况下访问它。例如,理想情况下,我会使用如下内容:

var featureCollection = {
  "type": "FeatureCollection",
  "features": [{
    "type": "Feature",
    "properties": {
      "id": 1
    },
    "geometry": {
      "type": "Point",
      "coordinates": [0, 0]
    }
  },{
    "type": "Feature",
    "properties": {
      "id": 2
    },
    "geometry": {
      "type": "Point",
      "coordinates": [30, 30]
    }
  },{
    "type": "Feature",
    "properties": {
      "id": 3
    },
    "geometry": {
      "type": "Point",
      "coordinates": [-30, -30]
    }
  }]
};
var geojson = L.geoJson(featureCollection);
var featureGroup = L.featureGroup().addTo(map);
featureGroup.addLayer(geojson);
var featureLayer = L.mapbox.featureLayer(featureCollection).addTo(map);
featureLayer.eachLayer(function (layer) {
  layer.on('click', function (e) {
    console.log('Clicked feature ID: ' + e.target.feature.properties.id);
  });
});
featureGroup.eachLayer(function (layer) {
  var id = layer.feature.properties.id;
  testfunction(id);
});

我还不知道怎么做。我尝试过在线搜索,但因为我是新手,我可能没有在搜索中使用正确的关键字。

geojson
嵌套在
featureGroup
中,因此当
eachLayer
函数运行时,它不会对
geojson
中的单个功能进行操作,而是在
geojson
本身上。要提取每个功能的
id
属性,您需要深入一层,在
geojson
中迭代功能

幸运的是,它还支持
eachLayer
方法(因为它是
L.FeatureGroup
的扩展,而L.LayerGroup本身就是
L.LayerGroup
的扩展)。要打印每个功能的
id
,您可以直接在
geojson
上使用
eachLayer
方法:

geojson.eachLayer(函数(层){
var id=layer.feature.properties.id;
testfunction(id);
});
或者,如果在
featureGroup
中嵌套了大量
L.GeoJson
对象,则可以使用:

featureGroup.eachLayer(函数(层){
layer.eachLayer(函数(层){
var id=layer.feature.properties.id;
testfunction(id);
});
});

当我们可以看到这一点时,你能发布一个JSFIDLE或其他示例演示站点吗?因为你建议的循环应该可以正常工作。这是我尝试做的一个例子。由于某种原因,地图没有显示出来。但无论如何,当我尝试这个时,它告诉我layer.feature是未定义的。这太棒了!谢谢!