Bentley Geo Web Publisher custom button to reset layers visibility

You work for a while in your Geo Web Site, you manipulated many layers, zoomed to a specific scale and panned to find what you were searching for but now you would like to restore the default layers visibility without having to refresh the entire Web site.  As the administrator of the Bentley Geo Web Publisher Web site, you can add a new button in the toolbar to bring the layers visibility back to the default startup display.

Create a new javascript file that will allow us to directly access the Map Viewer API (see the ResetLayers.txt file attached and rename it to ResetLayers.js).  The first step is to get the list of layers displayed at startup (and those that are not displayed too).  To do so, you must call the Map Viewer “GetNumLayers” method and then iterate through all layers to get which one is visible and which one is not.  This action must be done when the Geo Web Site is loaded.

    var numLayers = parent.mapViewerFunction('GetNumLayers');       for (layerIndex = 0; layerIndex < numLayers; layerIndex++)     {         var layerName = parent.mapViewerFunction('GetLayerName', layerIndex);         if (parent.mapViewerFunction('GetLayerVisible', layerName))             layersON.push(layerName);         else             layersOFF.push(layerName);     }

Layers default state is stored in “layersON” and “layersOFF” variables which are global javascript arrays.

Now you must add a new function to reset layers visibility.  This is the function that will be called when you click on the button.

    function ResetLayers()     {         for (var i = 0; i < layersON.length; i++)             parent.mapViewerFunction('SetLayerVisible', layersON[i], 1);           for (var i = 0; i < layersOFF.length; i++)             parent.mapViewerFunction('SetLayerVisible', layersOFF[i], 0);     }

The “SetLayerVisible” method sets the visible state of a layer.  The first parameter is a String that specifies the name of the layer and the second one is a Boolean that indicates whether the layer should be shown or hidden.

The final step consists to add a custom button in the Site Author.  Open your Geo Web Site (.dat file) and select the Viewer > Toolbar node to see its properties.  From the “Available Buttons” list, click on the “New” button and write the name of your button (“ResetLayers” in our case).  In the “Javascript Include File” property of the “Button Properties” section, select the ResetLayers.js file previously created.  Set the “OnClick Event” property to “ResetLayers” which is the name of the function to call in the javascript file.  Select a new icon that represents the current action and write a tooltip text description.  Move your new button to the right position in the Selected Buttons list and click Apply.

 

Save and Publish your Web site.

Any new buttons you create in the Site Author are saved in the “SiteAuthorToolbar.xml” configuration file located under “C:\Bentley\GeoWebPublisher\Administration”.  They are then available for all of your existing and future Geo Web Sites.