将 FeatureGroup 转换为 GeoJson 时,Leaflet Draw 不获取属性 [英] Leaflet Draw not taking properties when converting FeatureGroup to GeoJson

查看:15
本文介绍了将 FeatureGroup 转换为 GeoJson 时,Leaflet Draw 不获取属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

2 创建功能组: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 在每个功能中总是有空的属性属性,我无法弄清楚!

解决方案

iH8的初始答案几乎是正确的.p>

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

var myVectorLayer = L.rectangle(...)//不管var feature = myVectorLayer.feature = myVectorLayer.feature ||{};feature.type = "功能";特征.properties = 特征.properties ||{};feature.properties["Foo"] = "Bar";

现在 myVectorLayer.toGeoJSON() 返回一个有效的 GeoJSON 特征对象 表示为:

{类型":特征",特性": {Foo":酒吧"//更多可以预填充的属性.},"geometry"://向量几何}

I'm unable to convert my Layer properties into the properties of the GEOJson object using Leaflet(0.7.7)/Leaflet.Draw(latest). My workflow is:

1 Create Map: var map = L.map('#map', options);

2 Create a FeatureGroup: features= new L.FeatureGroup();

3 Add to the Leaflet Map: map.addLayer(features);

4 On the draw:created event, I'm capturing e.layer and adding a bunch of properties:

var layer = e.layer;

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

features.addLayer(layer);

geo_features = features.toGeoJSON();

However, my geo_features always have empty property attributes in each of the features and I can't figure it out!

解决方案

iH8's initial answer was almost correct.

To specify properties that will appear in a vector layer's GeoJSON export (i.e. through its .toGeoJSON() method), you have to fill its feature.type and feature.properties members:

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

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

Now myVectorLayer.toGeoJSON() returns a valid GeoJSON feature object represented by:

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

这篇关于将 FeatureGroup 转换为 GeoJson 时,Leaflet Draw 不获取属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆