From e7222c040c3830681f6cd4333c8ad21a4f5f9124 Mon Sep 17 00:00:00 2001 From: Olivier Terral Date: Mon, 23 Sep 2013 14:06:52 +0200 Subject: [PATCH] Fix styleUrl parsing --- examples/data/kml/lines.kml | 24 ++++++++++++++-------- examples/data/kml/styles.kml | 11 ++++++++++ src/ol/parser/kmlparser.js | 40 +++++++++++++++++------------------- 3 files changed, 46 insertions(+), 29 deletions(-) diff --git a/examples/data/kml/lines.kml b/examples/data/kml/lines.kml index adc8db6c55..cea9668e7a 100644 --- a/examples/data/kml/lines.kml +++ b/examples/data/kml/lines.kml @@ -262,14 +262,22 @@ -112.2626894973474,36.10149062823369,630 - - Blue Icon - Just another blue icon. - data/kml/styles.kml#blueIcons - - -112.292238941097,36.09520916122063,630 - - + + Blue Icon + Just another blue icon. + data/kml/styles.kml#blueIcons + + -112.292238941097,36.09520916122063,630 + + + + Sun Icon + Just another sun icon. + data/kml/styles.kml#sunIconMap + + -112.292238941097,36.15520916122063,630 + + diff --git a/examples/data/kml/styles.kml b/examples/data/kml/styles.kml index 24350ade44..b362878195 100644 --- a/examples/data/kml/styles.kml +++ b/examples/data/kml/styles.kml @@ -17,5 +17,16 @@ http://kml-samples.googlecode.com/svn/trunk/kml/Style/styles.kml + + + normal + #blueIcons + + + highlight + #blueIcons + + + diff --git a/src/ol/parser/kmlparser.js b/src/ol/parser/kmlparser.js index 2b63017708..b092cfa31f 100644 --- a/src/ol/parser/kmlparser.js +++ b/src/ol/parser/kmlparser.js @@ -148,12 +148,8 @@ ol.parser.KML = function(opt_options) { obj.features.push(feature); } } else if (goog.isDef(container.geometry)) { - var styleUrl = container.properties['styleUrl']; - if (goog.isDef(styleUrl)) { - if (!goog.string.startsWith(styleUrl, '#')) { - obj.links.push({href: styleUrl}); - } - } + this.parseStyleUrl(obj, container.properties['styleUrl']); + feature = new ol.Feature(container.properties); if (!goog.isNull(id)) { feature.setId(id); @@ -172,23 +168,17 @@ ol.parser.KML = function(opt_options) { } } var symbolizers = undefined; - if (goog.isDef(container.styleUrl)) { - feature.set('styleUrl', container.styleUrl); - - } else if (goog.isDef(container.styles)) { + if (goog.isDef(container.styles)) { symbolizers = container.styles[0].symbolizers; } else if (goog.isDef(container.styleMaps)) { var styleMap = container.styleMaps[0]; - for (var i = 0, ii = styleMap.pairs.length; i < ii; i++) { var pair = styleMap.pairs[i]; - if (pair.key === 'normal') { - if (goog.isDef(pair.styleUrl)) { + this.parseStyleUrl(obj, pair.styleUrl); feature.set('styleUrl', pair.styleUrl); - } else if (goog.isDef(pair.styles)) { symbolizers = pair.styles[0].symbolizers; } @@ -377,12 +367,6 @@ ol.parser.KML = function(opt_options) { this.readChildNodes(node, pair); obj['pairs'].push(pair); }, - 'key': function(node, obj) { - obj.key = this.getChildValue(node); - }, - 'styleUrl': function(node, obj) { - obj.styleUrl = this.getChildValue(node); - }, 'Style': function(node, obj) { if (this.extractStyles === true) { if (!obj['styles']) { @@ -960,6 +944,20 @@ ol.parser.KML.prototype.readFeaturesFromObject = }; +/** + * Parse the link contained in styleUrl, if it exists. + * @param {Object} obj The returned object from the parser. + * @param {String} styleUrl The style url to parse. + */ +ol.parser.KML.prototype.parseStyleUrl = function(obj, styleUrl) { + if (goog.isDef(styleUrl)) { + if (!goog.string.startsWith(styleUrl, '#')) { + obj.links.push({href: styleUrl}); + } + } +}; + + /** * @param {Array} deferreds List of deferred instances. * @param {Object} obj The returned object from the parser. @@ -1033,7 +1031,7 @@ ol.parser.KML.prototype.read = function(data, opt_callback) { function(datas) { for (var i = 0, ii = obj.features.length; i < ii; ++i) { var feature = obj.features[i]; - this.applyStyle_(feature, obj['styles']); + this.applyStyle_(feature, obj['styles'], obj['styleMaps']); } opt_callback.call(null, obj); }, function() {