I am trying to get JQuery and OpenLayers to play nicely together. Being a javascript newbie I am having trouble generating an opacity slider and visibility checkbox using jQuery which are associated with a given layer.
I do not know, before hand, how many layers will be available. This information is loaded via a python script which performs a WMS request to Geoserver.
I am coming from python and find that what I really want is a dictionary to store the layer object and a key. Here is my attempt to do this using a hash. I am looking for any suggestions, from "don't use a hash, look at "____" to "you missed the '()'.
The code below adds the layer to the map, it's name to the table of contents, an opacity bar, and a check box to set visibility. It works wonderfully for a single layer. Each subsequent layer overwrites the previous layers opacity slider and check box.
//If the thumbnail is clicked load the layer.
$('table').on('click','.thumbnail',function(event){
var layer = {};
//This info is placed into the img tag via jQuery
var name = ($(this).attr("layername_id"));
var title = ($(this).attr("titlename_id"));
//OpenLayers creates the layer here
layer[i] = new OpenLayers.Layer.WMS(title, "geoserver/wms?",{LAYERS: name,FORMAT: 'image/png',TRANSPARENT: 'true',PROJECTION:'EPSG:30100',BBOX: '-180,-60,180,60'},{OPACTIY: 0.1, isBaseLayer: false, tiled:true} );
//Do not add the same layer multiple times
if (map.getLayersByName(name) == false) {
map.addLayers([layer[i]]);
}
//Defined just to make life easier in this part of the code.
var name = layer[i].name;
//Check box for visibility in a table of contents DIV tag.
$('.toc').append('<div><input class="toc_inline" id="check'+i+'" type="checkbox" checked="checked"></div>');
$("[id^=check]").click(function(){
var $input = $(this);
if($input.prop('checked') == true){
layer[i].setVisibility(true);
}
else {
layer[i].setVisibility(false);
}
});
//Layer name in the TOC and slider to handle transparency.
$('.toc').append('<div class="toc_inline" unique_id="'+name+'">'+name+'</div>');
//Slider for transparency
$('.toc').append('<div id="slider-'+i+'"><div class="ui-slider-handle"></div></div>');
$("[id^=slider]").slider({
value: 100,
slide: function(e, ui) {
layer[i].setOpacity(ui.value / 100);
}
});
//My attempt to step to the next integer
i++;
});