Follow these steps to change layer properties:
Use the example derived from the first example from Chapter 1, Getting Started with OpenLayers, also available at chapter04/2360_04_01_changing_layer_properties.html:
<!doctype html>
<html lang="en">
<head>
<title>Simple example</title>
<link rel="stylesheet" href="../assets/ol3/css/ol.css" type="text/css" />
<link rel="stylesheet" href="../assets/css/samples.css" type="text/css" />
</head>
<body>
<div id="map" class="map"></div>
<script src="../assets/ol3/ol.js" ></script>
<script>
var osmLayer = new ol.layer.Tile({
source: new ol.source.OSM(),
opacity: 0.6,
brightness: 0.2
});
var view = new ol.View({
center: ol.proj.transform([-1.8118500054456526, 52.4431409750608], 'EPSG:4326', 'EPSG:3857'),
zoom: 6
});
var map = new ol.Map({
target: 'map'
});
map.addLayer(osmLayer);
map.setView(view);
</script>
</body>
</html>ol.source.Source from the osmLayer:console.log(osmLayer.getSource());
osmLayer.setProperties({opacity: 0.4, contrast:0.6});
console.log(osmLayer.get('contrast'));
console.log(osmLayer.get('opacity')); osmLayer.setProperties({opacity: 0.7, contrast:0.3});
console.log(osmLayer.getOpacity());
console.log(osmLayer.getContrast()); osmLayer.set('opacity',1);
osmLayer.setContrast(1);
osmLayer.setBrightness(0);
osmLayer.set('myId', 'myUnique');
console.log(osmLayer.get('myId'));
We reviewed how to manage main methods attached to the ol.layer.Layer subclass (remember that ol.layer.Tile inherits from it).
The interesting thing here, is that you are able to manipulate and create generic properties for all the ol.layer.Layer subclasses.
The most common operations on layers have their custom getters/setters such as getVisible() / setVisible(property), but you can replace them with the ol.layer.Layer getters/setters, get('visible')/set('visible', property). These are inherited from ol.Object, and consequently, ol.layer.Tile inherits them too. In the normal case, you use these getters/setters only for arbitrary properties we want to add, such as a name.
Another benefit you may not be aware of is that when you add new properties to a layer object, you can attach information for new applications related to layers. For example, you can set a property to reference metadata for the layer (intellectual properties, license, link to a PDF file to download, or more). Some other ways to use it are to define groups, set a unique identifier, set a display name to reuse in a custom layer manager, and set a reference to a legend resource (URL, image, and so on).
The possibilities are endless.
Now, after this short introduction to manage layer properties, we will review different raster layers and also the source property. It's mainly these elements that make the difference between raster layers in the OpenLayers 3 library.