Leaflet 将FeatureGroup转换为GeoJson时,传单绘图不带属性
我无法使用传单(0.7.7)/传单.Draw(最新版本)将我的图层属性转换为GEOJson对象的属性。我的工作流程是: 1创建映射:Leaflet 将FeatureGroup转换为GeoJson时,传单绘图不带属性,leaflet,leaflet.draw,Leaflet,Leaflet.draw,我无法使用传单(0.7.7)/传单.Draw(最新版本)将我的图层属性转换为GEOJson对象的属性。我的工作流程是: 1创建映射:var Map=L.Map(“#Map”,选项) 2创建FeatureGroup:features=new L.FeatureGroup() 3添加到传单地图:Map.addLayer(特征) 4在draw:created事件中,我正在捕获e.layer并添加一组属性: var层=e.layer layer.properties={Title:'Hello'} fe
var Map=L.Map(“#Map”,选项)代码>
2创建FeatureGroup:features=new L.FeatureGroup()代码>
3添加到传单地图:Map.addLayer(特征)代码>
4在draw:created
事件中,我正在捕获e.layer
并添加一组属性:
var层=e.layer代码>
layer.properties={Title:'Hello'}代码>
features.addLayer(层)代码>
geo_features=features.toGeoJSON()代码>
但是,我的geo_功能
在每个功能中总是有空的属性属性,我想不出来 A(一种难看的解决方法)使用L.GeoJSON
层,并使用其addData
方法将绘制层的GeoJSON添加到该层中。然后抓取L.GeoJSON
层的\u层
对象中的最后一层。此时,图层具有有效的GeoJSON功能
属性,您可以编辑:
var geojson=new L.geojson().addTo(map);
var drawControl=新的L.Control.Draw({
编辑:{
featureGroup:geojson
}
}).addTo(地图);
地图上('draw:created',函数(e){
geojson.addData(e.layer.toGeoJSON());
var layers=geojson.\u layers,
关键点=对象。关键点(层),
key=keys[keys.length-1],
图层=图层[关键点];
layer.feature.properties={
“Foo”:“Bar”
};
});
iH8几乎是正确的
要指定将出现在向量层GeoJSON导出中的属性(即通过其.toGeoJSON()
方法),必须填充其功能。键入和功能。属性成员:
var myVectorLayer=L.rectangle(…)//随便什么
var feature=myVectorLayer.feature=myVectorLayer.feature | |{};
feature.type=“feature”;
feature.properties=feature.properties | |{};
特性[“Foo”]=“Bar”;
现在myVectorLayer.toGeoJSON()
返回一个有效的值,表示为:
{
“类型”:“功能”,
“财产”:{
“Foo”:“Bar”
//更多可预填充的属性。
},
“几何体”://矢量几何体
}
对于您的L.GeoJSON调用,包括特性回调onEachFeature to options
L.GeoJSON(featureData,{onEachFeature:function(feature,layer){
//console.log(feature,layer);
// do something like
feature.setStyle( convertLayerOptionsFromFeatureProperties( feature.properties ) );
}} )
谢谢你的回复,但是我不能让它工作。注入e.layer.feature.properties时,它实际上在GEOJsonObject.features[0].geometry.properties中的级别太深,而不是在GEOJsonObject.features[0].properties中。这导致传单抛出“无效GEOJson错误,不呈现任何内容”哇,这太愚蠢了:(很抱歉造成混淆,我应该自己测试它。我现在明白了。我确信这会起作用。将我的答案更改为有效的,有点难看,但它确实起作用。让我知道您的想法。祝您好运!如果您还手动指定layer.feature.type=“feature”,则第一个版本会起作用。”
。我也被抓住了。@ghybs如果你把它作为答案发布,我可以这样标记:)