FIx style support in format.text to allow a set of style defaults.

Also, add an extractStyles option that can be configured to false to allow
th use of stylemaps. r=ahocevar (Closes #1844)


git-svn-id: http://svn.openlayers.org/trunk/openlayers@9271 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
crschmidt
2009-04-12 15:40:46 +00:00
parent 29ba60eb59
commit 29a23731ab
3 changed files with 98 additions and 5 deletions

View File

@@ -20,6 +20,25 @@
*/
OpenLayers.Format.Text = OpenLayers.Class(OpenLayers.Format, {
/**
* APIProperty: defaultStyle
* defaultStyle allows one to control the default styling of the features.
* It should be a symbolizer hash. By default, this is set to match the
* Layer.Text behavior, which is to use the default OpenLayers Icon.
*/
defaultStyle: null,
/**
* APIProperty: extractStyles
* set to true to extract styles from the TSV files, using information
* from the image or icon, iconSize and iconOffset fields. This will result
* in features with a symbolizer (style) property set, using the
* default symbolizer specified in <defaultStyle>. Set to false if you
* wish to use a styleMap or OpenLayers.Style options to style your
* layer instead.
*/
extractStyles: true,
/**
* Constructor: OpenLayers.Format.Text
* Create a new parser for TSV Text.
@@ -29,6 +48,19 @@ OpenLayers.Format.Text = OpenLayers.Class(OpenLayers.Format, {
* this instance.
*/
initialize: function(options) {
options = options || {};
if(options.extractStyles == true) {
options.defaultStyle = {
'externalGraphic': OpenLayers.Util.getImagesLocation() +
"marker.png",
'graphicXSize': 21,
'graphicYSize': 25,
'graphicXOffset': -10.5,
'graphicYOffset': -12.5
};
}
OpenLayers.Format.prototype.initialize.apply(this, [options]);
},
@@ -59,9 +91,12 @@ OpenLayers.Format.Text = OpenLayers.Class(OpenLayers.Format, {
var vals = currLine.split('\t');
var geometry = new OpenLayers.Geometry.Point(0,0);
var attributes = {};
var style = {};
var style = this.defaultStyle ?
OpenLayers.Util.applyDefaults({}, this.defaultStyle) :
null;
var icon, iconSize, iconOffset, overflow;
var set = false;
var styleSet = false;
for (var valIndex = 0; valIndex < vals.length; valIndex++) {
if (vals[valIndex]) {
if (columns[valIndex] == 'point') {
@@ -78,20 +113,27 @@ OpenLayers.Format.Text = OpenLayers.Class(OpenLayers.Format, {
} else if (columns[valIndex] == 'title')
attributes['title'] = vals[valIndex];
else if (columns[valIndex] == 'image' ||
columns[valIndex] == 'icon')
columns[valIndex] == 'icon' && style) {
style['externalGraphic'] = vals[valIndex];
else if (columns[valIndex] == 'iconSize') {
styleSet = true;
} else if (columns[valIndex] == 'iconSize' && style) {
var size = vals[valIndex].split(',');
style['graphicWidth'] = parseFloat(size[0]);
style['graphicHeight'] = parseFloat(size[1]);
} else if (columns[valIndex] == 'iconOffset') {
styleSet = true;
} else if (columns[valIndex] == 'iconOffset' && style) {
var offset = vals[valIndex].split(',');
style['graphicXOffset'] = parseFloat(offset[0]);
style['graphicYOffset'] = parseFloat(offset[1]);
styleSet = true;
} else if (columns[valIndex] == 'description') {
attributes['description'] = vals[valIndex];
} else if (columns[valIndex] == 'overflow') {
attributes['overflow'] = vals[valIndex];
} else {
// For StyleMap filtering, allow additional
// columns to be stored as attributes.
attributes[columns[valIndex]] = vals[valIndex];
}
}
}
@@ -99,7 +141,8 @@ OpenLayers.Format.Text = OpenLayers.Class(OpenLayers.Format, {
if (this.internalProjection && this.externalProjection) {
geometry.transform(this.externalProjection,
this.internalProjection);
}
}
style = styleSet ? style : null;
var feature = new OpenLayers.Feature.Vector(geometry, attributes, style);
features.push(feature);
}