Hi,
i want to add a layer dynamically to my openlayers map. The map is shown in a block. So i have generate a node, whith the fields layer and wms-url and a button. when i push the button i alert the fields for generating a wms-request.
So here is the javascript function i am using openlayers standalone (without drupal):

$('.btn').click(function(e)
{
alert(this.url); //wms-request-url
alert(this.layers); //all layers
var lgb_rlp = new OpenLayers.Layer.WMS
(
this.id,
this.url,
{
layers: this.layers,
format: "image/png",
exceptions: "application/vnd.ogc.se_inimage",
transparent: "true"
},
{
isBaseLayer: false,
visibility: true
}
);
map.addLayers([test]);
//alert(map); => object Object
}
);

So how can i get the map object and add an layer. I ´ve tested a lot of Drupal.openlayers.... or OpenLayers.map and so on. But it does not work.
Maybe some of you can help me.
Thanks a lot

Comments

Hi, here is the solution:
The problem was to get the real Openlayers map object

(function ($) {
    Drupal.behaviors.addlayerexample =
    {
        attach: function (data, options)
        {
                if (data == document) { //need to execute it once
                var layer = new OpenLayers.Layer.WMS
                (
                    'test',
                    'wms-url', //here use your normal wms-url
                        {
                        layers: 'layername',
                        format: "image/png",
                        exceptions: "application/vnd.ogc.se_inimage",
                        transparent: "true"
                    },
                    {
                        isBaseLayer: false,
                        visibility: true
                    }
                );
                var ol = $('#openlayers-map').data('openlayers'); //The problem was to get the real Openlayers map object-use jQuery
                ol.openlayers.addLayer( layer );
                }
        }
   };
})(jQuery);