问题
My KML File has the following format:
<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>
I want to be able to access the the extended data fields from the kml which I will be using for further processing and indexing the polygons for later use and searching.
As this post states, the variables can be access from placemark.vars.val but I could not get this since vars is undefined.
Load kml extendeddata into variable with Geoxml3
回答1:
Currently the kmz branch of geoxml3 handles ExtendedData
in the format defined in the KML Reference
If you modify your KML to that format (<Data>
instead of <SimpleData>
, <value>
tags around the value), it will work as is.
<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
The other option is to modify geoxml3 to support <SimpleData>
tags in your format (it is open source).
code snippet:
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>
回答2:
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');
来源:https://stackoverflow.com/questions/42516114/geoxml3-accessing-kml-attribute-datas