Friday, 10 April 2015

Not displaying any layers in OpenLayers


I am creating a map that contains some layers, all defined in an array called basemapsArray


var map = new ol.Map({
layers: basemapsArray,
controls : ol.control.defaults({
zoom : false
}),
target: 'map',
...


The layers need to be defined when the map is created, but none should be loaded right away; the user needs to pick one by clicking a checkbox first.


How would I go about not displaying any layers when the application loads?


I know I can loop through them and use .setVisible(), but perhaps there is a more elegant way?



Answer



Set visible to false while defining each layer as visible istrue by default.


{ visible: false }

Example:


var layers = [
new ol.layer.Tile({

source: new ol.source.OSM(),
visible: false
}),
new ol.layer.Tile({
extent: [-13884991, 2870341, -7455066, 6338219],
visible: false,
source: new ol.source.TileArcGISRest({
url: url
})
})

];

You can find the properties to be set for a layer in below:
http://openlayers.org/en/v3.6.0/apidoc/ol.layer.Base.html


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...