From 6abb691224c20c85d5df3fd7a35ffbb8f12fdd81 Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Fri, 7 Feb 2014 12:52:41 -0700 Subject: [PATCH] Give feature overlays a style option --- examples/image-vector-layer.js | 22 +++++++++------------- examples/modify-features.js | 2 +- examples/select-features.js | 32 +++++++++++++------------------- examples/vector-layer.js | 2 +- src/objectliterals.jsdoc | 2 +- src/ol/featureoverlay.js | 31 ++++++++++++++++++++++++++----- 6 files changed, 51 insertions(+), 40 deletions(-) diff --git a/examples/image-vector-layer.js b/examples/image-vector-layer.js index 342a5c9ac8..83bf47477b 100644 --- a/examples/image-vector-layer.js +++ b/examples/image-vector-layer.js @@ -42,21 +42,17 @@ var map = new ol.Map({ }) }); -var highlightStyleArray = [new ol.style.Style({ - stroke: new ol.style.Stroke({ - color: '#f00', - width: 1 - }), - fill: new ol.style.Fill({ - color: 'rgba(255,0,0,0.1)' - }) -})]; - var featureOverlay = new ol.FeatureOverlay({ map: map, - styleFunction: function(feature, resolution) { - return highlightStyleArray; - } + style: new ol.style.Style({ + stroke: new ol.style.Stroke({ + color: '#f00', + width: 1 + }), + fill: new ol.style.Fill({ + color: 'rgba(255,0,0,0.1)' + }) + }) }); var highlight; diff --git a/examples/modify-features.js b/examples/modify-features.js index 5d9fa52e32..ed8bb3714c 100644 --- a/examples/modify-features.js +++ b/examples/modify-features.js @@ -228,7 +228,7 @@ var overlayStyle = (function() { })(); var overlay = new ol.FeatureOverlay({ - styleFunction: overlayStyle + style: overlayStyle }); var modify = new ol.interaction.Modify({ featureOverlay: overlay }); diff --git a/examples/select-features.js b/examples/select-features.js index 27b76c63a4..def9194a29 100644 --- a/examples/select-features.js +++ b/examples/select-features.js @@ -15,34 +15,28 @@ var raster = new ol.layer.Tile({ source: new ol.source.MapQuest({layer: 'sat'}) }); -var unselectedStyle = [new ol.style.Style({ - fill: new ol.style.Fill({ - color: 'rgba(255,255,255,0.25)' - }), - stroke: new ol.style.Stroke({ - color: '#6666ff' - }) -})]; - -var selectedStyle = [new ol.style.Style({ - fill: new ol.style.Fill({ - color: 'rgba(255,255,255,0.5)' - }) -})]; - var vector = new ol.layer.Vector({ source: new ol.source.GeoJSON({ projection: 'EPSG:3857', url: 'data/geojson/countries.geojson' }), - style: unselectedStyle + style: new ol.style.Style({ + fill: new ol.style.Fill({ + color: 'rgba(255,255,255,0.25)' + }), + stroke: new ol.style.Stroke({ + color: '#6666ff' + }) + }) }); var select = new ol.interaction.Select({ featureOverlay: new ol.FeatureOverlay({ - styleFunction: function(feature, resolution) { - return selectedStyle; - } + style: new ol.style.Style({ + fill: new ol.style.Fill({ + color: 'rgba(255,255,255,0.5)' + }) + }) }) }); diff --git a/examples/vector-layer.js b/examples/vector-layer.js index 979b8732fb..ca33ead397 100644 --- a/examples/vector-layer.js +++ b/examples/vector-layer.js @@ -64,7 +64,7 @@ var highlightStyleCache = {}; var featureOverlay = new ol.FeatureOverlay({ map: map, - styleFunction: function(feature, resolution) { + style: function(feature, resolution) { var text = resolution < 5000 ? feature.get('name') : ''; if (!highlightStyleCache[text]) { highlightStyleCache[text] = [new ol.style.Style({ diff --git a/src/objectliterals.jsdoc b/src/objectliterals.jsdoc index 0b5d8ac1ad..7f1216abf4 100644 --- a/src/objectliterals.jsdoc +++ b/src/objectliterals.jsdoc @@ -566,7 +566,7 @@ * @typedef {Object} olx.FeatureOverlayOptions * @property {Array.|ol.Collection|undefined} features Features. * @property {ol.Map|undefined} map Map. - * @property {ol.feature.StyleFunction|undefined} styleFunction Style function. + * @property {ol.style.Style|Array.|ol.feature.StyleFunction|undefined} style Feature style. */ /** diff --git a/src/ol/featureoverlay.js b/src/ol/featureoverlay.js index ccc5f68b45..e5d2784af0 100644 --- a/src/ol/featureoverlay.js +++ b/src/ol/featureoverlay.js @@ -10,6 +10,7 @@ goog.require('ol.CollectionEventType'); goog.require('ol.Feature'); goog.require('ol.feature'); goog.require('ol.render.EventType'); +goog.require('ol.style.Style'); @@ -52,11 +53,35 @@ ol.FeatureOverlay = function(opt_options) { */ this.postComposeListenerKey_ = null; + /** + * @type {ol.feature.StyleFunction} + */ + var styleFunction; + if (goog.isDef(options.style)) { + if (goog.isFunction(options.style)) { + styleFunction = /** @type {ol.feature.StyleFunction} */ (options.style); + } else { + /** + * @type {Array.} + */ + var styles; + if (goog.isArray(options.style)) { + styles = options.style; + } else { + goog.asserts.assertInstanceof(options.style, ol.style.Style); + styles = [options.style]; + } + styleFunction = function(feature, resolution) { + return styles; + }; + } + } + /** * @private * @type {ol.feature.StyleFunction|undefined} */ - this.styleFunction_ = undefined; + this.styleFunction_ = styleFunction; if (goog.isDef(options.features)) { if (goog.isArray(options.features)) { @@ -69,10 +94,6 @@ ol.FeatureOverlay = function(opt_options) { this.setFeatures(new ol.Collection()); } - if (goog.isDef(options.styleFunction)) { - this.setStyleFunction(options.styleFunction); - } - if (goog.isDef(options.map)) { this.setMap(options.map); }