Merge pull request #4064 from fredj/lazy_ol.style.defaultStyleFunction

Use a private variable to cache the default style array
This commit is contained in:
Frédéric Junod
2015-09-07 09:24:52 +02:00

View File

@@ -239,48 +239,45 @@ ol.style.createStyleFunction = function(obj) {
};
/**
* @type {Array.<ol.style.Style>}
* @private
*/
ol.style.defaultStyle_ = null;
/**
* @param {ol.Feature} feature Feature.
* @param {number} resolution Resolution.
* @return {Array.<ol.style.Style>} Style.
*/
ol.style.defaultStyleFunction = 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. We don't use an immediately-invoked function
// We don't use an immediately-invoked function
// and a closure so we don't get an error at script evaluation time in
// browsers that do not support Canvas. (ol.style.Circle does
// canvas.getContext('2d') at construction time, which will cause an.error
// in such browsers.)
/**
* @param {ol.Feature} feature Feature.
* @param {number} resolution Resolution.
* @return {Array.<ol.style.Style>} Style.
*/
ol.style.defaultStyleFunction = function(feature, resolution) {
return styles;
};
return styles;
if (goog.isNull(ol.style.defaultStyle_)) {
var fill = new ol.style.Fill({
color: 'rgba(255,255,255,0.4)'
});
var stroke = new ol.style.Stroke({
color: '#3399CC',
width: 1.25
});
ol.style.defaultStyle_ = [
new ol.style.Style({
image: new ol.style.Circle({
fill: fill,
stroke: stroke,
radius: 5
}),
fill: fill,
stroke: stroke
})
];
}
return ol.style.defaultStyle_;
};