Use style option in vector layer examples

Providing a styleFunction option still works because the layer property is still named styleFunction.
This commit is contained in:
Tim Schaub
2014-02-07 10:43:19 -07:00
parent 5c21f24df5
commit 1072f6dfa7
16 changed files with 44 additions and 61 deletions

View File

@@ -118,7 +118,7 @@ dragAndDropInteraction.on('addfeatures', function(event) {
}); });
map.getLayers().push(new ol.layer.Vector({ map.getLayers().push(new ol.layer.Vector({
source: vectorSource, source: vectorSource,
styleFunction: styleFunction style: styleFunction
})); }));
var view2D = map.getView().getView2D(); var view2D = map.getView().getView2D();
view2D.fitExtent(vectorSource.getExtent(), map.getSize()); view2D.fitExtent(vectorSource.getExtent(), map.getSize());

View File

@@ -15,29 +15,25 @@ var raster = new ol.layer.Tile({
source: new ol.source.MapQuest({layer: 'sat'}) source: new ol.source.MapQuest({layer: 'sat'})
}); });
var styleArray = [new ol.style.Style({
fill: new ol.style.Fill({
color: 'rgba(255, 255, 255, 0.2)'
}),
stroke: new ol.style.Stroke({
color: '#ffcc33',
width: 2
}),
image: new ol.style.Circle({
radius: 7,
fill: new ol.style.Fill({
color: '#ffcc33'
})
})
})];
var source = new ol.source.Vector(); var source = new ol.source.Vector();
var vector = new ol.layer.Vector({ var vector = new ol.layer.Vector({
source: source, source: source,
styleFunction: function(feature, resolution) { style: new ol.style.Style({
return styleArray; fill: new ol.style.Fill({
} color: 'rgba(255, 255, 255, 0.2)'
}),
stroke: new ol.style.Stroke({
color: '#ffcc33',
width: 2
}),
image: new ol.style.Circle({
radius: 7,
fill: new ol.style.Fill({
color: '#ffcc33'
})
})
})
}); });
var map = new ol.Map({ var map = new ol.Map({

View File

@@ -177,7 +177,7 @@ vectorSource.addFeature(new ol.Feature(new ol.geom.Circle([5e6, 7e6], 1e6)));
var vectorLayer = new ol.layer.Vector({ var vectorLayer = new ol.layer.Vector({
source: vectorSource, source: vectorSource,
styleFunction: styleFunction style: styleFunction
}); });
var map = new ol.Map({ var map = new ol.Map({

View File

@@ -34,20 +34,15 @@ google.maps.event.addListenerOnce(gmap, 'tilesloaded', function() {
url: 'data/geojson/countries.geojson', url: 'data/geojson/countries.geojson',
projection: 'EPSG:3857' projection: 'EPSG:3857'
}), }),
styleFunction: (function() { style: new ol.style.Style({
var styleArray = [new ol.style.Style({ fill: new ol.style.Fill({
fill: new ol.style.Fill({ color: 'rgba(255, 255, 255, 0.6)'
color: 'rgba(255, 255, 255, 0.6)' }),
}), stroke: new ol.style.Stroke({
stroke: new ol.style.Stroke({ color: '#319FD3',
color: '#319FD3', width: 1
width: 1 })
}) })
})];
return function(feature, resolution) {
return styleArray;
};
}())
}); });
var center = gmap.getCenter(); var center = gmap.getCenter();

View File

@@ -48,7 +48,7 @@ var vector = new ol.layer.Vector({
projection: 'EPSG:3857', projection: 'EPSG:3857',
url: 'data/gpx/fells_loop.gpx' url: 'data/gpx/fells_loop.gpx'
}), }),
styleFunction: function(feature, resolution) { style: function(feature, resolution) {
return style[feature.getGeometry().getType()]; return style[feature.getGeometry().getType()];
} }
}); });

View File

@@ -63,7 +63,7 @@ var map = new ol.Map({
}), }),
new ol.layer.Vector({ new ol.layer.Vector({
source: vectorSource, source: vectorSource,
styleFunction: styleFunction style: styleFunction
}) })
], ],
renderer: 'canvas', renderer: 'canvas',

View File

@@ -55,7 +55,7 @@
<p id="shortdesc">Demonstrates the use of a Shape symbolizer to render earthquake locations.</p> <p id="shortdesc">Demonstrates the use of a Shape symbolizer to render earthquake locations.</p>
<div id="docs"> <div id="docs">
<p> <p>
This example parses a KML file and renders the features as a vector layer. The layer is given a <code>styleFunction</code> that renders earthquake locations with a size relative to their magnitude. This example parses a KML file and renders the features as a vector layer. The layer is given a <code>style</code> that renders earthquake locations with a size relative to their magnitude.
</p> </p>
<p>See the <a href="kml-earthquakes.js" target="_blank">kml-earthquakes.js source</a> to see how this is done.</p> <p>See the <a href="kml-earthquakes.js" target="_blank">kml-earthquakes.js source</a> to see how this is done.</p>
</div> </div>

View File

@@ -42,7 +42,7 @@ var vector = new ol.layer.Vector({
projection: 'EPSG:3857', projection: 'EPSG:3857',
url: 'data/kml/2012_Earthquakes_Mag5.kml' url: 'data/kml/2012_Earthquakes_Mag5.kml'
}), }),
styleFunction: styleFunction style: styleFunction
}); });
var raster = new ol.layer.Tile({ var raster = new ol.layer.Tile({

View File

@@ -49,7 +49,7 @@ var vector = new ol.layer.Vector({
projection: 'EPSG:3857', projection: 'EPSG:3857',
url: 'data/kml/timezones.kml' url: 'data/kml/timezones.kml'
}), }),
styleFunction: styleFunction style: styleFunction
}); });
var raster = new ol.layer.Tile({ var raster = new ol.layer.Tile({

View File

@@ -161,7 +161,7 @@ var vectorSource = new ol.source.GeoJSON(
var vectorLayer = new ol.layer.Vector({ var vectorLayer = new ol.layer.Vector({
source: vectorSource, source: vectorSource,
styleFunction: styleFunction style: styleFunction
}); });
var overlayStyle = (function() { var overlayStyle = (function() {

View File

@@ -83,16 +83,14 @@ for (i = 0, ii = extentsByDepth.length; i < ii; ++i) {
var vector = new ol.layer.Vector({ var vector = new ol.layer.Vector({
source: vectorSource, source: vectorSource,
styleFunction: function(feature, resolution) { style: styleArray
return styleArray;
}
}); });
var rtree = new ol.layer.Vector({ var rtree = new ol.layer.Vector({
source: new ol.source.Vector({ source: new ol.source.Vector({
features: rtreeExtentFeatures features: rtreeExtentFeatures
}), }),
styleFunction: function(feature, resolution) { style: function(feature, resolution) {
return feature.get('styleArray'); return feature.get('styleArray');
} }
}); });

View File

@@ -35,9 +35,7 @@ var vector = new ol.layer.Vector({
projection: 'EPSG:3857', projection: 'EPSG:3857',
url: 'data/geojson/countries.geojson' url: 'data/geojson/countries.geojson'
}), }),
styleFunction: function(feature, resolution) { style: unselectedStyle
return unselectedStyle;
}
}); });
var select = new ol.interaction.Select({ var select = new ol.interaction.Select({

View File

@@ -36,20 +36,16 @@ for (i = 0; i < count; ++i) {
startPoint = endPoint; startPoint = endPoint;
} }
var styleArray = [new ol.style.Style({
stroke: new ol.style.Stroke({
color: '#666666',
width: 1
})
})];
var vector = new ol.layer.Vector({ var vector = new ol.layer.Vector({
source: new ol.source.Vector({ source: new ol.source.Vector({
features: features features: features
}), }),
styleFunction: function(feature, resolution) { style: new ol.style.Style({
return styleArray; stroke: new ol.style.Stroke({
} color: '#666666',
width: 1
})
})
}); });
var view = new ol.View2D({ var view = new ol.View2D({

View File

@@ -46,7 +46,7 @@ var vectorSource = new ol.source.Vector({
}); });
var vector = new ol.layer.Vector({ var vector = new ol.layer.Vector({
source: vectorSource, source: vectorSource,
styleFunction: function(feature, resolution) { style: function(feature, resolution) {
return styles[feature.get('size')]; return styles[feature.get('size')];
} }
}); });

View File

@@ -30,7 +30,7 @@ var vector = new ol.layer.Vector({
projection: 'EPSG:3857', projection: 'EPSG:3857',
url: 'data/topojson/world-110m.json' url: 'data/topojson/world-110m.json'
}), }),
styleFunction: function(feature, resolution) { style: function(feature, resolution) {
// don't want to render the full world polygon, which repeats all countries // don't want to render the full world polygon, which repeats all countries
return feature.getId() !== undefined ? styleArray : null; return feature.getId() !== undefined ? styleArray : null;
} }

View File

@@ -17,7 +17,7 @@ var vectorLayer = new ol.layer.Vector({
projection: 'EPSG:3857', projection: 'EPSG:3857',
url: 'data/geojson/countries.geojson' url: 'data/geojson/countries.geojson'
}), }),
styleFunction: function(feature, resolution) { style: function(feature, resolution) {
var text = resolution < 5000 ? feature.get('name') : ''; var text = resolution < 5000 ? feature.get('name') : '';
if (!styleCache[text]) { if (!styleCache[text]) {
styleCache[text] = [new ol.style.Style({ styleCache[text] = [new ol.style.Style({