I have a leaflet map and a kml
file. I load the kml file using the plugin: leaflet-omnivore. Now I want to access the <folder> <name>
(which is "RegionName"). I need it because I want to link the kml
with an other json
file which also has RegionName
as a properties.name
. But when output the loaded kml file via console.log(myKML)
I can only access the <name>
below <placemark>
The code I load the kml with:
var myKML = omnivore.kml('KML/Testdaten_KML.kml', null, L.mapbox.featureLayer());
The kml file is structured like:
<Folder>
<name>RegionName</name>
<Placemark>
<name>Form 1</name>
<Polygon>
<outerBoundaryIs>
<LinearRing>
<tessellate>1</tessellate>
<coordinates>
11.82,52.60
11.86,52.58
11.90,52.61
11.87,52.62
11.82,52.60
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
</Placemark>
</Folder>
Best Answer
So this is one possible solution, it is a bit long but straightforward!
Step 1: Create a global variable ("kmlJSON") and load your KML via a JQUERY AJAX call (or vanilla JS, as you prefer):
Step 2: Enter this function ('xmlToJson') as is, taken from here:
Step 3: You can then access the KML as a JSON. Normally, it should be:
but assuming the KML snippet you attached is the whole KML then it should just be: