OL3:提取扩展数据'来自KML

时间:2015-07-16 10:13:16

标签: javascript kml openlayers-3

我创建了一个KML文件,其中包含一些自定义属性,例如' id'和' room_id'。

这些属性存在于'扩展数据中。 KML文件的一部分,这是文件:

<?xml version="1.0" encoding="utf-8" ?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document id="root_doc">
<Schema name="map" id="map">
    <SimpleField name="id" type="int"></SimpleField>
    <SimpleField name="room_id" type="string"></SimpleField>
</Schema>
<Folder><name>map</name>
  <Placemark>
    <name>room1</name>
    <Style><LineStyle><color>ff0000ff</color></LineStyle><PolyStyle><fill>0</fill></PolyStyle></Style>
    <ExtendedData><SchemaData schemaUrl="#map">
        <SimpleData name="id">2</SimpleData>
        <SimpleData name="room_id">1</SimpleData>
    </SchemaData></ExtendedData>
      <Polygon><altitudeMode>relativeToGround</altitudeMode><outerBoundaryIs><LinearRing><altitudeMode>relativeToGround</altitudeMode> <coordinates>-0.000010931306469,0.000006764422322 -0.000004193941839,0.000006764422322 -0.000004166884149,0.00000037880765 -0.000010985421848,0.00000043292302 -0.000010931306469,0.000006764422322</coordinates></LinearRing></outerBoundaryIs></Polygon>
  </Placemark>
</Folder>
</Document></kml>

我正在尝试使用OL3访问这些属性,但无法弄清楚。这是我用来访问标准KML属性的代码&#39; name&#39;和&#39;描述&#39;:

vector.getSource().forEachFeature(function (feature) {

     var desc = feature.getProperties()['description'];
     console.log(desc);

     var name = feature.getProperties()['name'];
     console.log(name);

});

如何访问扩展数据&#39; OL3的KML文件的属性?

谢谢!

1 个答案:

答案 0 :(得分:1)

这很简单:

 var properties = feature.getProperties();
 console.log(properties['id']);
 console.log(properties['room_id']);