Example 2 is a widget that is designed to be called as a command from a menu. It requires Mozilla Firefox and the Firebug extension because it writes to the Firebug console.
It uses the following files in the folder WebServerExtensionsInstallDir\www\fusion:
This example writes information about the currently selected parcels to the Firebug console. For more information about selections, see The Map Component and Working With Selections.
In a flexible web layout, the current selection is available from the map object using the getSelection() method. This is an asynchronous method that accepts a callback routine as a parameter. For example, the following sets up a callback for the current selection:
var theMap = Fusion.getWidgetById("Map"); theMap.getSelection(this.displaySelection );
The callback method must accept a single parameter, which is a selection object:
displaySelection : function(selection){ // process the selection object }
It is possible for a single selection object to contain selected features from multiple maps and multiple layers in those maps. The selection object passed to the callback routine is an associative array where each element in the array has the selection data for a single map.
To get the selection for a single map, get the element corresponding to that map. This is usually the main map. For example:
var theMap = Fusion.getWidgetById("Map"); if (!theMap.hasSelection()) { alert("Nothing selected"); return; } var oSelection = selection[theMap.getMapName()];
To get the selected features for a layer, call either getLayerByName() or getLayer(). For example, to get the selected features for the Parcels layer, call
var thisLayer = oSelection.getLayerByName('Parcels');
To loop through all the layers in a selection, do the following:
for (var layerNum = 0; layerNum < oSelection.getNumLayers(); layerNum++) { var thisLayer = oSelection.getLayer(layerNum); var selectedFeaturesThisLayer = thisLayer.getNumElements(); if (selectedFeaturesThisLayer > 0) { // Process the selected features } }
To process each selected feature, do the following:
for (var featureNum = 0; featureNum < selectedFeaturesThisLayer; featureNum++) { // process feature }
The layer definition in Infrastructure Studio defines what properties of the selected features are available to the JavaScript API on the client. Each property has a name and a value.
var propNames = thisLayer.getPropertyNames(); var nProperties = thisLayer.getNumProperties(); for (var propNum = 0; propNum < nProperties; propNum++) { // process property var thisPropName = propNames[propNum]; var thisPropValue = thisLayer.getElementValue(featureNum, ownerPropNum) }
Example2.js combines these concepts to write the owner names of selected parcels to the Firebug console.