Fix styleUrl parsing

This commit is contained in:
Olivier Terral
2013-09-23 14:06:52 +02:00
parent 39e56f3e61
commit e7222c040c
3 changed files with 46 additions and 29 deletions

View File

@@ -262,14 +262,22 @@
-112.2626894973474,36.10149062823369,630 </coordinates>
</LineString>
</Placemark>
<Placemark>
<name>Blue Icon</name>
<description>Just another blue icon.</description>
<styleUrl>data/kml/styles.kml#blueIcons</styleUrl>
<Point>
<coordinates>-112.292238941097,36.09520916122063,630</coordinates>
</Point>
</Placemark>
<Placemark>
<name>Blue Icon</name>
<description>Just another blue icon.</description>
<styleUrl>data/kml/styles.kml#blueIcons</styleUrl>
<Point>
<coordinates>-112.292238941097,36.09520916122063,630</coordinates>
</Point>
</Placemark>
<Placemark>
<name>Sun Icon</name>
<description>Just another sun icon.</description>
<styleUrl>data/kml/styles.kml#sunIconMap</styleUrl>
<Point>
<coordinates>-112.292238941097,36.15520916122063,630</coordinates>
</Point>
</Placemark>
</Folder>
</Document>
</kml>

View File

@@ -17,5 +17,16 @@ http://kml-samples.googlecode.com/svn/trunk/kml/Style/styles.kml
</IconStyle>
</Style>
<StyleMap id="sunIconMap">
<Pair>
<key>normal</key>
<styleUrl>#blueIcons</styleUrl>
</Pair>
<Pair>
<key>highlight</key>
<styleUrl>#blueIcons</styleUrl>
</Pair>
</StyleMap>
</Document>
</kml>

View File

@@ -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() {