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

查看:181
本文介绍了将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的初始答案几乎是正确的.

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

 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功能对象,表示为:

 {
  "type": "Feature",
  "properties": {
    "Foo": "Bar"
    // More properties that may be pre-filled.
  },
  "geometry": // The vector 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天全站免登陆