Give precedence to feature style

This commit is contained in:
Antoine Abt
2014-07-10 12:11:02 +02:00
parent 0b9936107d
commit 60f1874766
14 changed files with 172 additions and 176 deletions
+43
View File
@@ -0,0 +1,43 @@
goog.provide('ol.style.defaults');
goog.require('ol.style.Circle');
goog.require('ol.style.Fill');
goog.require('ol.style.Stroke');
goog.require('ol.style.Style');
/**
* @param {ol.Feature} feature Feature.
* @param {number} resolution Resolution.
* @return {Array.<ol.style.Style>} Style.
*/
ol.style.defaults.styleFunction = function(feature, resolution) {
var fill = new ol.style.Fill({
color: 'rgba(255,255,255,0.4)'
});
var stroke = new ol.style.Stroke({
color: '#3399CC',
width: 1.25
});
var styles = [
new ol.style.Style({
image: new ol.style.Circle({
fill: fill,
stroke: stroke,
radius: 5
}),
fill: fill,
stroke: stroke
})
];
// now that we've run it the first time,
// replace the function with a constant version
ol.style.defaults.styleFunction =
/** @type {function(this:ol.Feature):Array.<ol.style.Style>} */(
function(resolution) {
return styles;
});
return styles;
};
+46
View File
@@ -1,5 +1,7 @@
goog.provide('ol.style.Style');
goog.require('goog.asserts');
goog.require('goog.functions');
goog.require('ol.style.Fill');
goog.require('ol.style.Image');
@@ -93,3 +95,47 @@ ol.style.Style.prototype.getText = function() {
ol.style.Style.prototype.getZIndex = function() {
return this.zIndex_;
};
/**
* A function that takes an {@link ol.Feature} and a `{number}` representing
* the view's resolution. The function should return an array of
* {@link ol.style.Style}. This way e.g. a vector layer can be styled.
*
* @typedef {function(ol.Feature, number): Array.<ol.style.Style>}
* @api
*/
ol.style.StyleFunction;
/**
* Convert the provided object into a style function. Functions passed through
* unchanged. Arrays of ol.style.Style or single style objects wrapped in a
* new style function.
* @param {ol.style.StyleFunction|Array.<ol.style.Style>|ol.style.Style} obj
* A style function, a single style, or an array of styles.
* @return {ol.style.StyleFunction} A style function.
*/
ol.style.createStyleFunction = function(obj) {
/**
* @type {ol.style.StyleFunction}
*/
var styleFunction;
if (goog.isFunction(obj)) {
styleFunction = /** @type {ol.style.StyleFunction} */ (obj);
} else {
/**
* @type {Array.<ol.style.Style>}
*/
var styles;
if (goog.isArray(obj)) {
styles = obj;
} else {
goog.asserts.assertInstanceof(obj, ol.style.Style);
styles = [obj];
}
styleFunction = goog.functions.constant(styles);
}
return styleFunction;
};