[英]Convert a newly created polygon to GeoJSON in Leaflet
I create polygons of different sizes by clicking a button in my webapp. 我通过在Web应用程序中单击一个按钮来创建不同大小的多边形。
I also add some values inside the object as a nested object, like {properties:{status:'active'}}
. 我还在对象内部添加了一些值作为嵌套对象,例如{properties:{status:'active'}}
。 Then I run toGeoJSON()
method of the polygon and get an object with properties
and geometry
objects. 然后,我运行多边形的toGeoJSON()
方法,并获得具有properties
和geometry
对象的对象。 properties
object is empty. properties
对象为空。
My question is how do I add my values into the object so that they are passed to the GeoJSON object on conversion? 我的问题是如何将值添加到对象中,以便在转换时将其传递给GeoJSON对象?
Any "extra" data on your Polygon is lost when a GeoJSON object is created for it. 为它创建GeoJSON对象时,多边形上的所有“额外”数据都会丢失。 Only the coordinates of the polygon are carried over into the GeoJSON object. 只有多边形的坐标才被带入GeoJSON对象。 See lines 213 and 171 in layer/GeoJSON.js in the Leaflet source. 请参阅Leaflet源中layer / GeoJSON.js中的第213和171行。 On line 171 you can see that a new object is created and that object has an attribute called "properties" but that properties attribute has nothing to do with any attribute called "properties" on your Polygon. 在171行上,您可以看到创建了一个新对象,并且该对象具有一个称为“属性”的属性,但该属性与您的多边形上与任何名为“属性”的属性无关。
After creating the GeoJSON object you could copy the properties from your Polygon into the properties object on the GeoJSON object by doing something like the following. 创建GeoJSON对象后,您可以通过执行以下操作将属性从“多边形”复制到GeoJSON对象上的属性对象中。 However - I'm not sure what, if any, specific meaning the "properties" object has in the GeoJSON specification. 但是-我不确定GeoJSON规范中“属性”对象的具体含义(如果有)。
var json = polygon.toGeoJSON();
L.extend(json.properties, polygon.properties)
InPursuits answer definitely worked for me... InPursuits答案肯定对我有用...
var rect = L.rectangle(bounds).toGeoJSON();
L.extend(rect.properties, {
itemIndex: v.itemIndex
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.