Skip to content

Commit

Permalink
Merge branch 'master' of falcacibar/leaflet-plugins
Browse files Browse the repository at this point in the history
Conflicts:
	layer/vector/KML.js
  • Loading branch information
fbuccioni committed Jul 14, 2015
2 parents d14dfb1 + ad5b53d commit bcad78d
Showing 1 changed file with 72 additions and 14 deletions.
86 changes: 72 additions & 14 deletions layer/vector/KML.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
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',
onEachFeature: null
},

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);
}
},

Expand All @@ -36,7 +44,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', {
Expand All @@ -53,8 +61,11 @@ L.KML = L.FeatureGroup.extend({

L.Util.extend(L.KML, {

parseKML: function (xml) {
var style = this.parseStyles(xml);
parseKML: function (xml, options) {
if(options !== undefined)
this.options = options

var style = this.parseStyle(xml);
this.parseStyleMap(xml, style);
var el = xml.getElementsByTagName('Folder');
var layers = [], l;
Expand Down Expand Up @@ -250,28 +261,75 @@ L.Util.extend(L.KML, {
if (!layers.length) {
return;
}

var layer = layers[0];

if (layers.length > 1) {
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('<h2>' + name + '</h2>' + 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('<h2>' + name + '</h2>' + descr);

if(typeof(this.options.onEachFeature) === 'function')
this.options.onEachFeature(layer.feature, layer);

return layer;
},
Expand Down

0 comments on commit bcad78d

Please sign in to comment.