Leaflet在将FeatureGroup转换为GeoJson时不绘制属性

时间:2016-03-02 23:14:27

标签: leaflet leaflet.draw

我无法使用Leaflet(0.7.7)/Leaflet.Draw(最新)将我的图层属性转换为GEOJson对象的属性。我的工作流程是:

1创建地图:var map = L.map('#map', options);

2创建一个FeatureGroup:features= new L.FeatureGroup();

3添加到宣传单地图:map.addLayer(features);

4在draw:created事件中,我抓取e.layer并添加了一堆属性:

var layer = e.layer;

layer.properties = { Title: 'Hello' };

features.addLayer(layer);

geo_features = features.toGeoJSON();

但是,我的geo_features在每个功能中始终都有空属性属性,我无法弄明白!

3 个答案:

答案 0 :(得分:8)

iH8' initial answer几乎是正确的。

要指定将出现在矢量图层的GeoJSON导出中的属性(即通过其.toGeoJSON()方法),您必须填充其feature.typefeature.properties成员:< / p>

var myVectorLayer = L.rectangle(...) // whatever

var feature = myVectorLayer.feature = myVectorLayer.feature || {};
feature.type = "Feature";
feature.properties = feature.properties || {};
feature.properties["Foo"] = "Bar";

现在myVectorLayer.toGeoJSON()会返回一个有效的GeoJSON feature object代表:

{
  "type": "Feature",
  "properties": {
    "Foo": "Bar"
    // More properties that may be pre-filled.
  },
  "geometry": // The vector geometry
}

答案 1 :(得分:2)

A(一种丑陋的解决方法)正在使用L.GeoJSON图层,并使用它的addData方法将绘制的图层的GeoJSON添加到其中。然后抓取L.GeoJSON图层的_layers对象中的最后一个图层。此时,图层具有您可以编辑的有效GeoJSON feature属性:

var geojson = new L.GeoJSON().addTo(map);

var drawControl = new L.Control.Draw({
    edit: {
        featureGroup: geojson
    }
}).addTo(map);

map.on('draw:created', function (e) {

    geojson.addData(e.layer.toGeoJSON());

    var layers = geojson._layers,
        keys = Object.keys(layers),
        key = keys[keys.length - 1],
        layer = layers[key];

    layer.feature.properties = {
        'Foo': 'Bar'
    };
});

答案 2 :(得分:0)

对于您的L.GeoJSON调用,将onEachFeature的功能回调包括到选项中

    L.GeoJSON(featureData,{onEachFeature:function(feature,layer){

 //console.log(feature,layer);
 // do something like 

 feature.setStyle( convertLayerOptionsFromFeatureProperties( feature.properties ) );

}} )