0

我的 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 未定义,因此我无法获得。

使用 Geoxml3 将 kml 扩展数据加载到变量中

4

2 回答 2

1

目前 geoxml3 的 kmz 分支以KML 参考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>

概念证明小提琴

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

代码片段:

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>';
html,
body,
#map_canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<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>

于 2017-03-09T06:16:15.533 回答
0

Replying to geocodezip reply above...

You approach works but the problem is the kml is produced frequently. Making the changes you mentioned might be a bit difficult because of the content of the <SimpleData> tag which contains the value as text where the expected format is <Data><value></value></Data>.

Best way would be to create a php or any program that parse the kml and make the changes. I decided to edit the GeoXML3.js parser to look for SimpleData instead of Data tags at line number 639

var dataNodes = getElementsByTagName(extDataNodes[0], 'Data');
于 2017-03-09T12:11:26.367 回答