From bd6f10eed18384a06a6f71371f960793bb280041 Mon Sep 17 00:00:00 2001 From: Felipe A Buccioni Date: Mon, 20 Jul 2015 02:25:45 -0300 Subject: [PATCH 1/3] KML: Inherited options --- layer/vector/KML.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/layer/vector/KML.js b/layer/vector/KML.js index acdc4e8..7a7a178 100644 --- a/layer/vector/KML.js +++ b/layer/vector/KML.js @@ -4,12 +4,12 @@ L.KML = L.FeatureGroup.extend({ }, initialize: function(kml, options) { - L.Util.setOptions(this, options); + this.options = L.Util.setOptions(this, options); this._kml = kml; this._layers = {}; if (kml) { - this.addKML(kml, options, this.options.async); + this.addKML(kml, this.options, this.options.async); } }, @@ -54,7 +54,7 @@ L.KML = L.FeatureGroup.extend({ }, _addKML: function(xml, options) { - var layers = L.KML.parseKML(xml); + var layers = L.KML.parseKML(xml, options); if (!layers || !layers.length) return; for (var i = 0; i < layers.length; i++) { this.fire('addlayer', { @@ -70,8 +70,10 @@ L.KML = L.FeatureGroup.extend({ }); L.Util.extend(L.KML, { + parseKML: function (xml, options) { + if(options !== undefined) + this.options = options; - parseKML: function (xml) { var style = this.parseStyles(xml); this.parseStyleMap(xml, style); var el = xml.getElementsByTagName('Folder'); From 0969aafe381cc614b762fb6218006d70d1324a45 Mon Sep 17 00:00:00 2001 From: Felipe A Buccioni Date: Mon, 20 Jul 2015 02:46:58 -0300 Subject: [PATCH 2/3] KML: Added feature property, parse of ExtendedData and manipulation of name, description and displayName tags in properties --- layer/vector/KML.js | 67 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 58 insertions(+), 9 deletions(-) diff --git a/layer/vector/KML.js b/layer/vector/KML.js index 7a7a178..59c69a4 100644 --- a/layer/vector/KML.js +++ b/layer/vector/KML.js @@ -1,6 +1,13 @@ L.KML = L.FeatureGroup.extend({ options: { - async: true + async: true, + popup: true, + displayName: false, + displayName_suffix: '_display_name', + name_on_props : false, + name_prop : 'kml_name', + descr_on_props : false, + descr_prop : 'kml_description' }, initialize: function(kml, options) { @@ -275,23 +282,65 @@ L.Util.extend(L.KML, { layer = new L.FeatureGroup(layers); } - var name, descr = ''; - el = place.getElementsByTagName('name'); + this.parseData(place, layer) + + return layer; + }, + + parseData: function(xml, layer) { + var name, + descr = '', + props = {}, + eld, eldn, el, i, j, prop + ; + + el = xml.getElementsByTagName('ExtendedData') + for (i = 0; i < el.length; i++) { + for (j = 0; j < el[i].childNodes.length; j++) { + eld = el[i].childNodes[j]; + + if(eld.tagName === 'Data') { + prop = eld.getAttribute('name'); + props[prop] = null + + eldn = eld.getElementsByTagName('value') + if(eldn.length) + props[prop] = eldn[0].firstChild.nodeValue; + + if(this.options.displayName) { + eldn = eld.getElementsByTagName('displayName') + if(eldn.length) + props[prop + this.options.displayName_suffix] = eldn[0].firstChild.nodeValue; + } + } + } + } + + el = xml.getElementsByTagName('name'); if (el.length && el[0].childNodes.length) { name = el[0].childNodes[0].nodeValue; } - el = place.getElementsByTagName('description'); + + el = xml.getElementsByTagName('description'); for (i = 0; i < el.length; i++) { for (j = 0; j < el[i].childNodes.length; j++) { descr = descr + el[i].childNodes[j].nodeValue; } } - if (name) { - layer.on('add', function(e) { - layer.bindPopup('

' + name + '

' + descr); - }); - } + layer.name = name; + if(this.options.name_on_props) + props[this.options.name_prop] = name; + + layer.description = descr; + if(this.options.descr_on_props) + props[this.options.descr_prop] = descr; + + layer.feature = layer.toGeoJSON(); + layer.feature.properties = props; + + if(this.options.popup && ( name || descr )) + layer.bindPopup('

' + name + '

' + descr); return layer; }, From e8e66f4463745d229244956d1bb9b92eca18328c Mon Sep 17 00:00:00 2001 From: Felipe A Buccioni Date: Mon, 20 Jul 2015 02:48:58 -0300 Subject: [PATCH 3/3] KML: Added onEachFeature support --- layer/vector/KML.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/layer/vector/KML.js b/layer/vector/KML.js index 59c69a4..e3170d3 100644 --- a/layer/vector/KML.js +++ b/layer/vector/KML.js @@ -7,7 +7,8 @@ L.KML = L.FeatureGroup.extend({ name_on_props : false, name_prop : 'kml_name', descr_on_props : false, - descr_prop : 'kml_description' + descr_prop : 'kml_description', + onEachFeature: null }, initialize: function(kml, options) { @@ -342,6 +343,9 @@ L.Util.extend(L.KML, { if(this.options.popup && ( name || descr )) layer.bindPopup('

' + name + '

' + descr); + if(typeof(this.options.onEachFeature) === 'function') + this.options.onEachFeature(layer.feature, layer); + return layer; },