GeoXML3访问KML属性数据

时间:2017-02-28 18:24:00

标签: javascript xml kml geoxml3

我的KML文件格式如下:

<Placemark>
<Style><LineStyle><color>ff0000ff</color></LineStyle><PolyStyle><fill>0</fill></PolyStyle></Style>
<ExtendedData><SchemaData schemaUrl="#seb">
    <SimpleData name="PR0">CORS</SimpleData>
    <SimpleData name="PR1">BRB</SimpleData>
    <SimpleData name="PR2">F15</SimpleData>
</SchemaData></ExtendedData>
  <MultiGeometry><Polygon><altitudeMode>clampToGround</altitudeMode><outerBoundaryIs><LinearRing><altitudeMode>clampToGround</altitudeMode><coordinates>71.0035714700001,38.4757616580001 71.0567352510001,38.398144523 71.1035044220001,38.422803406000138.4764993150001 71.0035714700001,38.4757616580001</coordinates></LinearRing></outerBoundaryIs></Polygon></MultiGeometry>

我希望能够从kml访问扩展数据字段,我将用它来进一步处理和索引多边形以供以后使用和搜索。

正如这篇文章所述,变量可以从placemark.vars.val访问,但由于vars未定义,我无法得到它。

Load kml extendeddata into variable with Geoxml3

2 个答案:

答案 0 :(得分:1)

目前geoxml3的kmz分支以KML Reference

中定义的格式处理ExtendedData

如果您将KML修改为该格式(<Data>而不是<SimpleData><value>标记值,则它将按原样运行。

<Placemark>
  <Style>
    <LineStyle><color>ff0000ff</color><width>2</width></LineStyle>
    <PolyStyle><fill>0</fill></PolyStyle>
    <BalloonStyle>
      <text>
      <![CDATA[$[name]<br>PR0: $[PR0]<br>PR1: $[PR1]<br>PR2: $[PR2]]]>
      </text>
    </BalloonStyle>
  </Style>
  <ExtendedData>
  <SchemaData schemaUrl="#seb">
    <Data name="PR0">
      <value>CORS</value>
    </Data>
    <Data name="PR1">
      <value>BRB</value>
    </Data>
    <Data name="PR2">
      <value>F15</value>
    </Data></SchemaData>
  </ExtendedData>
  <MultiGeometry>
    <Polygon>
      <altitudeMode>clampToGround</altitudeMode>
      <outerBoundaryIs><LinearRing>
        <altitudeMode>clampToGround</altitudeMode>
        <coordinates>71.0035714700001,38.4757616580001 71.0567352510001,38.398144523 71.1035044220001,38.422803406000138.4764993150001 71.0035714700001,38.4757616580001
        </coordinates>
      </LinearRing></outerBoundaryIs>
    </Polygon>
  </MultiGeometry>
</Placemark>

proof of concept fiddle

另一种选择是修改geoxml3以支持您格式的<SimpleData>标记(它是开源的)。

代码段

&#13;
&#13;
var geocoder;
var map;

function initialize() {
  var map = new google.maps.Map(
    document.getElementById("map_canvas"), {
      center: new google.maps.LatLng(37.4419, -122.1419),
      zoom: 13,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
  var geoXml = new geoXML3.parser({
    map: map,
    singleInfoWindow: true
  });
  geoXml.parseKmlString(kmlStr);
}
google.maps.event.addDomListener(window, "load", initialize);
var kmlStr = '<Placemark><Style><LineStyle><color>ff0000ff</color><width>2</width></LineStyle><PolyStyle><fill>0</fill></PolyStyle><BalloonStyle><text><![CDATA[$[name]<br>PR0: $[PR0]<br>PR1: $[PR1]<br>PR2: $[PR2]]]></text></BalloonStyle></Style><ExtendedData><SchemaData schemaUrl="#seb"><Data name="PR0"><value>CORS</value></Data><Data name="PR1"><value>BRB</value></Data><Data name="PR2"><value>F15</value></Data></SchemaData></ExtendedData><MultiGeometry><Polygon><altitudeMode>clampToGround</altitudeMode><outerBoundaryIs><LinearRing><altitudeMode>clampToGround</altitudeMode><coordinates>71.0035714700001,38.4757616580001 71.0567352510001,38.398144523 71.1035044220001,38.422803406000138.4764993150001 71.0035714700001,38.4757616580001</coordinates></LinearRing></outerBoundaryIs></Polygon></MultiGeometry></Placemark>';
&#13;
html,
body,
#map_canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
&#13;
<script src="https://maps.googleapis.com/maps/api/js"></script>
<script src="https://cdn.rawgit.com/geocodezip/geoxml3/master/kmz/geoxml3.js"></script>
<div id="map_canvas"></div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

回复上面的 geocodezip 回复...

你接近工作,但问题是经常产生kml。由于<SimpleData>标记的内容包含作为预期格式为<Data><value></value></Data>的文本的值,因此您提及的更改可能会有点困难。

最好的方法是创建一个php或任何解析kml并进行更改的程序。我决定编辑GeoXML3.js解析器以在第639行查找SimpleData而不是Data标签

var dataNodes = getElementsByTagName(extDataNodes[0], 'Data');