I am trying to dynamically add and manage mulitple graphic layers in my map. When a user clicks a button, I want to fire off a function that runs a query task, creating a new graphicslayer as needed. In addition, I want to dynamically populate a datagrid with the names of the facilities in that layer.
This all works just fine the first time through. However, if the user turns off the layer and then turns it back on, I really didn't want to rerun the query task. Instead I wanted to loop through all the layers in the map and if it found that the layer existed, it would just turn it back on. This still worked until I added the funtionality of populating the data grid. Because I'm looping through all the layers in the map with
for each (var mapLayer:Layer in thisMapLayers) {
I need to use the generic Layer instead of GraphicsLayer (because there are more than GraphicsLayer in my map). As it loops through, it will find the right GraphicsLayer, but since it's typed as a Layer, it doesn't pass successfully as a parameter. Instead I'm getting the coercion error because I'm passing a Layer and I need GraphicsLayer. Is there anyway to copy/clone/change the Layer to a GraphicsLayer?
var checkGLayerName:String = layer+"Layer";
var thisMapLayers:Object = mainMap.layers;
if (mapLayer.name == checkGLayerName)
{
mapLayer.visible=true;
listCheck = true;
populateFeatureList(layer,mapLayer);
}
private function populateFeatureList(mapFeature:String, mapLayer:GraphicsLayer):void {
var myGraphicsLayer:GraphicsLayer = mapLayer;
// var featureListAcc:Accordion = new Accordion;
featureListAcc.percentHeight=100;
featureListAcc.percentWidth=100;
featureListAcc.id = "featureListAcc"
featureListPanel.visible=true;
featureListPanel.includeInLayout=true;
featureListPanel.addChild(featureListAcc);
// var vBoxes:Array = [];
var featureListBox:VBox = new VBox;
featureListBox.id = mapFeature+"Box";
featureListBox.label=mapFeature; // **** need a better label than this
featureListBox.percentWidth=100;
// featureListAcc.addChild(featureListBox);
vBoxes.push(featureListAcc.addChild(featureListBox));
featureListAcc.resizeToContent=true;
// mapCanvas.percentWidth=80;
var featureDataGrid:DataGrid = new DataGrid;
featureDataGrid.id = mapFeature+"_dg";
featureDataGrid.percentWidth=100;
var columns:Array = [];
var myDataGridColumn:DataGridColumn = new DataGridColumn ("Facility");
myDataGridColumn.headerText = mapFeature+" Names";
columns.push (myDataGridColumn);
//maybe don't always add cities? really confusing with air ambulance without it ...
/* var myDataGridColumn2:DataGridColumn = new DataGridColumn ("City");
myDataGridColumn2.width=15;
columns.push (myDataGridColumn2); */
featureDataGrid.columns=columns;
//---------------------------------------------
var myExtent : Extent = mainMap.extent;
var graphic : Graphic;
var results : ArrayCollection = new ArrayCollection; //this is the list for the datagrid
var featuresInExtent:int = 0
// checks the features in the current map extent and only populates the list with the ones in the extent
for (var i : Number = 0 ; i < myGraphicsLayer.numChildren ; i++)
{
graphic = myGraphicsLayer.getChildAt(i) as Graphic;
if (myExtent.contains(MapPoint(graphic.geometry)))
{
results.addItem(graphic.attributes); //all the attributes are in the arrayCollection, even though only one column
//is currently shown in the datagrid
featuresInExtent++;
}
// info.text = "The map shows " + featuresInExtent + " site(s)." ;
}
//sorts the data alphapbetically before the grid is populated otherwise it is in the order it was loaded in the original data
var dataSortField:SortField = new SortField ();
dataSortField.name = "Facility"; //FACILITY if data is in SDE !!!!
var dataSort:Sort = new Sort();
dataSort.fields = [dataSortField];
results.sort = dataSort;
results.refresh();
featureDataGrid.dataProvider = results;
//-----------------------------------------------------------------
featureListBox.addChild(featureDataGrid);
featureListPanel.percentWidth=25;
} // end populateFeatureList function