Give feature overlays a style option

This commit is contained in:
Tim Schaub
2014-02-07 12:52:41 -07:00
parent b309c44020
commit 6abb691224
6 changed files with 51 additions and 40 deletions

View File

@@ -42,7 +42,9 @@ var map = new ol.Map({
}) })
}); });
var highlightStyleArray = [new ol.style.Style({ var featureOverlay = new ol.FeatureOverlay({
map: map,
style: new ol.style.Style({
stroke: new ol.style.Stroke({ stroke: new ol.style.Stroke({
color: '#f00', color: '#f00',
width: 1 width: 1
@@ -50,13 +52,7 @@ var highlightStyleArray = [new ol.style.Style({
fill: new ol.style.Fill({ fill: new ol.style.Fill({
color: 'rgba(255,0,0,0.1)' color: 'rgba(255,0,0,0.1)'
}) })
})]; })
var featureOverlay = new ol.FeatureOverlay({
map: map,
styleFunction: function(feature, resolution) {
return highlightStyleArray;
}
}); });
var highlight; var highlight;

View File

@@ -228,7 +228,7 @@ var overlayStyle = (function() {
})(); })();
var overlay = new ol.FeatureOverlay({ var overlay = new ol.FeatureOverlay({
styleFunction: overlayStyle style: overlayStyle
}); });
var modify = new ol.interaction.Modify({ featureOverlay: overlay }); var modify = new ol.interaction.Modify({ featureOverlay: overlay });

View File

@@ -15,34 +15,28 @@ var raster = new ol.layer.Tile({
source: new ol.source.MapQuest({layer: 'sat'}) source: new ol.source.MapQuest({layer: 'sat'})
}); });
var unselectedStyle = [new ol.style.Style({ var vector = new ol.layer.Vector({
source: new ol.source.GeoJSON({
projection: 'EPSG:3857',
url: 'data/geojson/countries.geojson'
}),
style: new ol.style.Style({
fill: new ol.style.Fill({ fill: new ol.style.Fill({
color: 'rgba(255,255,255,0.25)' color: 'rgba(255,255,255,0.25)'
}), }),
stroke: new ol.style.Stroke({ stroke: new ol.style.Stroke({
color: '#6666ff' 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
}); });
var select = new ol.interaction.Select({ var select = new ol.interaction.Select({
featureOverlay: new ol.FeatureOverlay({ featureOverlay: new ol.FeatureOverlay({
styleFunction: function(feature, resolution) { style: new ol.style.Style({
return selectedStyle; fill: new ol.style.Fill({
} color: 'rgba(255,255,255,0.5)'
})
})
}) })
}); });

View File

@@ -64,7 +64,7 @@ var highlightStyleCache = {};
var featureOverlay = new ol.FeatureOverlay({ var featureOverlay = new ol.FeatureOverlay({
map: map, map: map,
styleFunction: function(feature, resolution) { style: function(feature, resolution) {
var text = resolution < 5000 ? feature.get('name') : ''; var text = resolution < 5000 ? feature.get('name') : '';
if (!highlightStyleCache[text]) { if (!highlightStyleCache[text]) {
highlightStyleCache[text] = [new ol.style.Style({ highlightStyleCache[text] = [new ol.style.Style({

View File

@@ -566,7 +566,7 @@
* @typedef {Object} olx.FeatureOverlayOptions * @typedef {Object} olx.FeatureOverlayOptions
* @property {Array.<ol.Feature>|ol.Collection|undefined} features Features. * @property {Array.<ol.Feature>|ol.Collection|undefined} features Features.
* @property {ol.Map|undefined} map Map. * @property {ol.Map|undefined} map Map.
* @property {ol.feature.StyleFunction|undefined} styleFunction Style function. * @property {ol.style.Style|Array.<ol.style.Style>|ol.feature.StyleFunction|undefined} style Feature style.
*/ */
/** /**

View File

@@ -10,6 +10,7 @@ goog.require('ol.CollectionEventType');
goog.require('ol.Feature'); goog.require('ol.Feature');
goog.require('ol.feature'); goog.require('ol.feature');
goog.require('ol.render.EventType'); goog.require('ol.render.EventType');
goog.require('ol.style.Style');
@@ -52,11 +53,35 @@ ol.FeatureOverlay = function(opt_options) {
*/ */
this.postComposeListenerKey_ = null; 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.<ol.style.Style>}
*/
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 * @private
* @type {ol.feature.StyleFunction|undefined} * @type {ol.feature.StyleFunction|undefined}
*/ */
this.styleFunction_ = undefined; this.styleFunction_ = styleFunction;
if (goog.isDef(options.features)) { if (goog.isDef(options.features)) {
if (goog.isArray(options.features)) { if (goog.isArray(options.features)) {
@@ -69,10 +94,6 @@ ol.FeatureOverlay = function(opt_options) {
this.setFeatures(new ol.Collection()); this.setFeatures(new ol.Collection());
} }
if (goog.isDef(options.styleFunction)) {
this.setStyleFunction(options.styleFunction);
}
if (goog.isDef(options.map)) { if (goog.isDef(options.map)) {
this.setMap(options.map); this.setMap(options.map);
} }