Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Fix calls to parseFolder to stop processing folders multiple times; call... #3

Open
wants to merge 5 commits into
from
View
@@ -5,12 +5,12 @@ L.KML = L.FeatureGroup.extend({
L.Util.setOptions(this, options);
this._kml = kml;
this._layers = {};
-
+
if (kml) {
this.addKML(kml);
}
},
-
+
addKML: function(kml) {
var req = new window.XMLHttpRequest();
req.open('GET', kml, false);
@@ -19,17 +19,27 @@ L.KML = L.FeatureGroup.extend({
if (req.status != 200) return;
var layers = L.KML.parseKML(req.responseXML);
for (var i = 0; i < layers.length; i++)
+ {
+ this.fire('addlayer', {
+ layer: layers[i]
+ });
this.addLayer(layers[i]);
- }
+ }
+ this.latLngs = L.KML.getLatLngs(req.responseXML);
+ },
+
+ latLngs: Array()
});
L.Util.extend(L.KML, {
+
parseKML: function(xml) {
var style = this.parseStyle(xml);
var el = xml.getElementsByTagName("Folder");
var layers = [];
for (var i = 0; i < el.length; i++) {
- var l = this.parseFolder(xml, style);
+ if (!this._check_folder(el[i])) continue;
+ var l = this.parseFolder(el[i], style);
if (l) layers.push(l);
}
el = xml.getElementsByTagName('Placemark');
@@ -41,11 +51,13 @@ L.Util.extend(L.KML, {
return layers;
},
+ // Return false if e's first parent Folder is not [folder]
+ // - returns true if no parent Folders
_check_folder: function(e, folder) {
e = e.parentElement;
- while (e && e.tagName != "Folder" && e != folder)
+ while (e && e.tagName != "Folder")
e = e.parentElement;
- return e || e == folder;
+ return !e || e == folder;
},
parseStyle: function(xml) {
@@ -161,14 +173,7 @@ L.Util.extend(L.KML, {
parseCoords: function(xml) {
var el = xml.getElementsByTagName('coordinates');
- var coords = [];
- var text = el[0].childNodes[0].nodeValue.split(/[\s\n]+/);
- for (var i = 0; i < text.length; i++) {
- var ll = text[i].split(',');
- if (ll.length < 2) continue;
- coords.push(new L.LatLng(ll[1], ll[0]));
- }
- return coords;
+ return this._read_coords(el[0]);
},
parseLineString: function(line, xml, options) {
@@ -200,6 +205,31 @@ L.Util.extend(L.KML, {
if (options.fillColor) options.fill = true;
if (polys.length == 1) return new L.Polygon(polys.concat(inner), options);
return new L.MultiPolygon(polys, options);
+ },
+
+ getLatLngs: function(xml) {
+ var el = xml.getElementsByTagName('coordinates');
+ var coords = [];
+ for (var j = 0; j < el.length; j++) {
+ // text might span many childnodes
+ coords = coords.concat(this._read_coords(el[j]));
+ }
+ return coords;
+ },
+
+ _read_coords: function(el) {
+ var text = "";
+ var coords = [];
+ for (var i = 0; i < el.childNodes.length; i++) {
+ text = text + el.childNodes[i].nodeValue;
+ }
+ text = text.split(/[\s\n]+/);
+ for (var i = 0; i < text.length; i++) {
+ var ll = text[i].split(',');
+ if (ll.length < 2) continue;
+ coords.push(new L.LatLng(ll[1], ll[0]));
+ }
+ return coords;
}
});