Parse the KML LabelStyle and use the color and opacity value. Create a parseKmlColor function to avoid code duplication. r=pgiraud (closes #2413)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@10007 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Frédéric Junod
2010-02-03 10:23:19 +00:00
parent 2100d63846
commit c75e16e5f2
2 changed files with 53 additions and 28 deletions

View File

@@ -277,13 +277,38 @@ OpenLayers.Format.KML = OpenLayers.Class(OpenLayers.Format.XML, {
for(var i=0, len=nodes.length; i<len; i++) {
var style = this.parseStyle(nodes[i]);
if(style) {
styleName = (options.styleBaseUrl || "") + "#" + style.id;
var styleName = (options.styleBaseUrl || "") + "#" + style.id;
this.styles[styleName] = style;
}
}
},
/**
* Method: parseKmlColor
* Parses a kml color (in 'aabbggrr' format) and returns the corresponding
* color and opacity or null if the color is invalid.
*
* Parameters:
* kmlColor - {String} a kml formated color
*
* Returns:
* {Object}
*/
parseKmlColor: function(kmlColor) {
var color = null;
if (kmlColor) {
var matches = kmlColor.match(this.regExes.kmlColor);
if (matches) {
color = {
color: '#' + matches[4] + matches[3] + matches[2],
opacity: parseInt(matches[1], 16) / 255
};
}
}
return color;
},
/**
* Method: parseStyle
* Parses the children of a <Style> node and builds the style hash
@@ -296,7 +321,8 @@ OpenLayers.Format.KML = OpenLayers.Class(OpenLayers.Format.XML, {
parseStyle: function(node) {
var style = {};
var types = ["LineStyle", "PolyStyle", "IconStyle", "BalloonStyle"];
var types = ["LineStyle", "PolyStyle", "IconStyle", "BalloonStyle",
"LabelStyle"];
var type, nodeList, geometry, parser;
for(var i=0, len=types.length; i<len; ++i) {
type = types[i];
@@ -309,20 +335,11 @@ OpenLayers.Format.KML = OpenLayers.Class(OpenLayers.Format.XML, {
// only deal with first geometry of this type
switch (type.toLowerCase()) {
case "linestyle":
var color = this.parseProperty(styleTypeNode, "*", "color");
var kmlColor = this.parseProperty(styleTypeNode, "*", "color");
var color = this.parseKmlColor(kmlColor);
if (color) {
var matches = (color.toString()).match(
this.regExes.kmlColor);
// transparency
var alpha = matches[1];
style["strokeOpacity"] = parseInt(alpha, 16) / 255;
// rgb colors (google uses bgr)
var b = matches[2];
var g = matches[3];
var r = matches[4];
style["strokeColor"] = "#" + r + g + b;
style["strokeColor"] = color.color;
style["strokeOpacity"] = color.opacity;
}
var width = this.parseProperty(styleTypeNode, "*", "width");
@@ -332,20 +349,11 @@ OpenLayers.Format.KML = OpenLayers.Class(OpenLayers.Format.XML, {
break;
case "polystyle":
var color = this.parseProperty(styleTypeNode, "*", "color");
var kmlColor = this.parseProperty(styleTypeNode, "*", "color");
var color = this.parseKmlColor(kmlColor);
if (color) {
var matches = (color.toString()).match(
this.regExes.kmlColor);
// transparency
var alpha = matches[1];
style["fillOpacity"] = parseInt(alpha, 16) / 255;
// rgb colors (google uses bgr)
var b = matches[2];
var g = matches[3];
var r = matches[4];
style["fillColor"] = "#" + r + g + b;
style["fillOpacity"] = color.opacity;
style["fillColor"] = color.color;
}
// Check if fill is disabled
var fill = this.parseProperty(styleTypeNode, "*", "fill");
@@ -477,6 +485,15 @@ OpenLayers.Format.KML = OpenLayers.Class(OpenLayers.Format.XML, {
this.regExes.straightBracket, "${$1}");
}
break;
case "labelstyle":
var kmlColor = this.parseProperty(styleTypeNode, "*", "color");
var color = this.parseKmlColor(kmlColor);
if (color) {
style["fontColor"] = color.color;
style["fontOpacity"] = color.opacity;
}
break;
default:
}
}