NewToActionscript
11-14-2009, 10:29 PM
Hi,
I'm new to flex programming but have some exposure to basic programming, have downloaded code below which finds and displays fields in an attribute table of feature, seems to be loading this into array collection. The problem I have is that it displays it in a random order, and I need it displayed in the order the fields are in the table, or in alphabetical order. Tried to do a sort on the array collection but didn't work, any ideas would be really appreciated.
Many thanks,
[CODE]
<mx:Script>
<![CDATA[
import com.esri.ags.events.DrawEvent;
import com.esri.ags.events.IdentifyEvent;
import com.esri.ags.geometry.Extent;
import com.esri.ags.geometry.Geometry;
import com.esri.ags.geometry.MapPoint;
import com.esri.ags.geometry.Polygon;
import com.esri.ags.geometry.Polyline;
import com.esri.ags.Graphic;
import com.esri.ags.layers.ArcGISTiledMapServiceLayer;
import com.esri.ags.layers.ArcGISDynamicMapServiceLayer;
import com.esri.ags.layers.GraphicsLayer;
import com.esri.ags.layers.Layer;
import com.esri.ags.Map;
import com.esri.ags.symbol.PictureMarkerSymbol;
import com.esri.ags.symbol.SimpleFillSymbol;
import com.esri.ags.symbol.SimpleLineSymbol;
import com.esri.ags.tasks.FeatureSet;
import com.esri.ags.tasks.IdentifyParameters;
import com.esri.ags.tasks.IdentifyResult;
import com.esri.ags.tasks.IdentifyTask;
import com.esri.ags.tasks.Query;
import com.esri.ags.tasks.QueryTask;
import com.esri.ags.toolbars.Draw;
import com.esri.solutions.flexviewer.SiteContainer;
import com.esri.solutions.flexviewer.utils.WidgetEffects;
import mx.collections.*;
import mx.controls.Alert;
import mx.rpc.AsyncResponder;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
//labels
private var identifyLabel:String;
private var resultsLabel:String;
[Bindable]
private var descriptionLabel:String;
private var identifyLayerOption:String;
private var identifyTolerance:Number = 3;
[Bindable]
private var pointLabel:String;
[Bindable]
private var clearLabel:String;
private var loadingLabel:String;
private var selectionLabel:String;
[Bindable]
private var msgVisible:Boolean = false;
private var zoomScale:Number = 5000;
private var graphicsLayer:GraphicsLayer;
private var graphicPointSym:PictureMarkerSymbol;
private var identifyPoint:MapPoint;
[Bindable]
private var identifyArrayCollection:ArrayCollection;
private const ICON_URL:String = "com/esri/solutions/flexviewer/assets/images/icons/";
private function init():void
{
graphicPointSym = new PictureMarkerSymbol(widgetIcon, 30, 30)
graphicsLayer = new GraphicsLayer();
graphicsLayer.symbol = graphicPointSym;
map.addLayer(graphicsLayer);
if (configXML)
{
//labels
identifyLabel = configXML.labels.graphicalsearchlabel || "Identify";
resultsLabel = configXML.labels.resultslabel || "Results";
descriptionLabel = configXML.labels.descriptionlabel || "Use the identify tool to identify features on the map:";
pointLabel = configXML.labels.pointlabel || "Identify";
clearLabel = configXML.labels.clearlabel || "Clear";
loadingLabel = configXML.labels.loadinglabel || "Loading...";
selectionLabel = configXML.labels.selectionlabel || "Features Selected:";
identifyLayerOption = configXML.identifylayeroption || IdentifyParameters.LAYER_OPTION_VISIBLE;
if (Number(configXML.identifytolerance) > 0)
identifyTolerance = Number(configXML.identifytolerance);
if (Number(configXML.zoomscale) > 0)
zoomScale = Number(configXML.zoomscale);
}
wTemplate.addTitlebarButton(ICON_URL + "i_table.png", resultsLabel, showStateResults);
wTemplate.addTitlebarButton(ICON_URL + "i_info.png", identifyLabel, showStateIdentify);
activateIdentifyTool();
}
private function activateIdentifyTool():void
{
var status:String = pointLabel;
setMapAction(Draw.MAPPOINT, status, drawEnd);;
}
private function drawEnd(event:DrawEvent):void
{
clear();
graphicsLayer.add(event.graphic);
identifyPoint = event.graphic.geometry as MapPoint;
identifyFeatures();
}
//identify features
private function identifyFeatures():void
{
var identifyParams : IdentifyParameters = new IdentifyParameters();
identifyParams.returnGeometry = false;
identifyParams.tolerance = identifyTolerance;
identifyParams.geometry = identifyPoint;
identifyParams.width = map.width;
identifyParams.height = map.height;
identifyParams.layerOption = identifyLayerOption;
identifyParams.mapExtent = map.extent;
for (var i:Number = map.layerIds.length -1; i >= 0; i--)
{
var layer:Layer = map.getLayer(map.layerIds[i]);
var url:String;
if (layer.visible)
{
if (layer is ArcGISDynamicMapServiceLayer)
{
var dynamicLayer:ArcGISDynamicMapServiceLayer = layer as ArcGISDynamicMapServiceLayer;
url = dynamicLayer.url;
}
else if (layer is ArcGISTiledMapServiceLayer)
{
var tiledLayer:ArcGISTiledMapServiceLayer = layer as ArcGISTiledMapServiceLayer;
url = tiledLayer.url;
}
if(url)
{
var identifyTask:IdentifyTask = new IdentifyTask(url);
identifyTask.addEventListener(IdentifyEvent.IDENTI FY_COMPLETE, onResult);
identifyTask.addEventListener(FaultEvent.FAULT, onFault);
identifyTask.execute(identifyParams);
showMessage(loadingLabel, true);
showStateResults(null);
}
}
}
}
// on result
private function onResult(event:IdentifyEvent):void
{
try
{
processIdentifyResults(event.identifyResults);
}
catch (error:Error)
{
showMessage(error.message, false);
}
}
//on fault
private function onFault(event:FaultEvent):void
{
showMessage(event.fault.faultDetail, false);
}
//proocess identify results
private function processIdentifyResults(identifyResults:Array):void
{
if(!identifyArrayCollection)
identifyArrayCollection = new ArrayCollection();
for each (var identifyResult:IdentifyResult in identifyResults)
{
var title:String = identifyResult.layerName;
var obj:Object = identifyResult.feature.attributes;
var content:String = "";
var fld:String;
var value:String;
for (fld in obj)
{
value = obj[fld].toString();
content += fld + ": " + value + "\n";
}
var infoData:Object =
{
icon: widgetIcon,
title: identifyResult.layerName,
content: content,
link: "",
point: identifyPoint,
geometry: identifyPoint
};
identifyArrayCollection.addItem(infoData);
}
clearMessage();
}
private function clear():void
{
graphicsLayer.clear();
clearMessage();
identifyArrayCollection = null;
identifyPoint = null;
this.showInfoWindow(null);
}
private var hitimer:uint;
private function mouseOverRecord(event:MouseEvent):void
{
var infoData:Object = event.currentTarget.infoData;
clearTimeout(hitimer);
hitimer = setTimeout(showHighlight, 800, [infoData]);
}
private function mouseOutRecord():void
{
clearTimeout(hitimer);
this.showInfoWindow(null);
}
private function clickRecord(event:MouseEvent):void
{
var infoData:Object = event.currentTarget.infoData;
var pt:MapPoint = infoData.point;
if (map.scale > zoomScale)
map.scale = zoomScale;
map.centerAt(pt);
}
private function showHighlight(params:Array):void
{
var infoData:Object = params[0];
var pt:MapPoint = infoData.point;
//if (!map.extent.contains(pt))
// map.centerAt(pt);
this.showInfoWindow(params[0]);
}
private function showMessage(msg:String, swfVisible:Boolean):void
{
txtMessage.text = msg;
swfMessage.visible = swfVisible;
msgVisible = true;
}
private function clearMessage():void
{
msgVisible = false;
}
private function widgetClosedHandler(event:Event):void
{
graphicsLayer.visible = false;
this.showInfoWindow(null);
setMapNavigation(null, null);
}
private function widgetOpenedHandler(event:Event):void
{
activateIdentifyTool();
graphicsLayer.visible = true;
}
private function showStateIdentify(event:MouseEvent):void
{
WidgetEffects.flipWidget(this, viewStack, "selectedIndex", 0, 400);
}
private function showStateResults(event:MouseEvent):void
{
WidgetEffects.flipWidget(this, viewStack, "selectedIndex", 1, 400);
}
]]>
</mx:Script>
[CODE]
I'm new to flex programming but have some exposure to basic programming, have downloaded code below which finds and displays fields in an attribute table of feature, seems to be loading this into array collection. The problem I have is that it displays it in a random order, and I need it displayed in the order the fields are in the table, or in alphabetical order. Tried to do a sort on the array collection but didn't work, any ideas would be really appreciated.
Many thanks,
[CODE]
<mx:Script>
<![CDATA[
import com.esri.ags.events.DrawEvent;
import com.esri.ags.events.IdentifyEvent;
import com.esri.ags.geometry.Extent;
import com.esri.ags.geometry.Geometry;
import com.esri.ags.geometry.MapPoint;
import com.esri.ags.geometry.Polygon;
import com.esri.ags.geometry.Polyline;
import com.esri.ags.Graphic;
import com.esri.ags.layers.ArcGISTiledMapServiceLayer;
import com.esri.ags.layers.ArcGISDynamicMapServiceLayer;
import com.esri.ags.layers.GraphicsLayer;
import com.esri.ags.layers.Layer;
import com.esri.ags.Map;
import com.esri.ags.symbol.PictureMarkerSymbol;
import com.esri.ags.symbol.SimpleFillSymbol;
import com.esri.ags.symbol.SimpleLineSymbol;
import com.esri.ags.tasks.FeatureSet;
import com.esri.ags.tasks.IdentifyParameters;
import com.esri.ags.tasks.IdentifyResult;
import com.esri.ags.tasks.IdentifyTask;
import com.esri.ags.tasks.Query;
import com.esri.ags.tasks.QueryTask;
import com.esri.ags.toolbars.Draw;
import com.esri.solutions.flexviewer.SiteContainer;
import com.esri.solutions.flexviewer.utils.WidgetEffects;
import mx.collections.*;
import mx.controls.Alert;
import mx.rpc.AsyncResponder;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
//labels
private var identifyLabel:String;
private var resultsLabel:String;
[Bindable]
private var descriptionLabel:String;
private var identifyLayerOption:String;
private var identifyTolerance:Number = 3;
[Bindable]
private var pointLabel:String;
[Bindable]
private var clearLabel:String;
private var loadingLabel:String;
private var selectionLabel:String;
[Bindable]
private var msgVisible:Boolean = false;
private var zoomScale:Number = 5000;
private var graphicsLayer:GraphicsLayer;
private var graphicPointSym:PictureMarkerSymbol;
private var identifyPoint:MapPoint;
[Bindable]
private var identifyArrayCollection:ArrayCollection;
private const ICON_URL:String = "com/esri/solutions/flexviewer/assets/images/icons/";
private function init():void
{
graphicPointSym = new PictureMarkerSymbol(widgetIcon, 30, 30)
graphicsLayer = new GraphicsLayer();
graphicsLayer.symbol = graphicPointSym;
map.addLayer(graphicsLayer);
if (configXML)
{
//labels
identifyLabel = configXML.labels.graphicalsearchlabel || "Identify";
resultsLabel = configXML.labels.resultslabel || "Results";
descriptionLabel = configXML.labels.descriptionlabel || "Use the identify tool to identify features on the map:";
pointLabel = configXML.labels.pointlabel || "Identify";
clearLabel = configXML.labels.clearlabel || "Clear";
loadingLabel = configXML.labels.loadinglabel || "Loading...";
selectionLabel = configXML.labels.selectionlabel || "Features Selected:";
identifyLayerOption = configXML.identifylayeroption || IdentifyParameters.LAYER_OPTION_VISIBLE;
if (Number(configXML.identifytolerance) > 0)
identifyTolerance = Number(configXML.identifytolerance);
if (Number(configXML.zoomscale) > 0)
zoomScale = Number(configXML.zoomscale);
}
wTemplate.addTitlebarButton(ICON_URL + "i_table.png", resultsLabel, showStateResults);
wTemplate.addTitlebarButton(ICON_URL + "i_info.png", identifyLabel, showStateIdentify);
activateIdentifyTool();
}
private function activateIdentifyTool():void
{
var status:String = pointLabel;
setMapAction(Draw.MAPPOINT, status, drawEnd);;
}
private function drawEnd(event:DrawEvent):void
{
clear();
graphicsLayer.add(event.graphic);
identifyPoint = event.graphic.geometry as MapPoint;
identifyFeatures();
}
//identify features
private function identifyFeatures():void
{
var identifyParams : IdentifyParameters = new IdentifyParameters();
identifyParams.returnGeometry = false;
identifyParams.tolerance = identifyTolerance;
identifyParams.geometry = identifyPoint;
identifyParams.width = map.width;
identifyParams.height = map.height;
identifyParams.layerOption = identifyLayerOption;
identifyParams.mapExtent = map.extent;
for (var i:Number = map.layerIds.length -1; i >= 0; i--)
{
var layer:Layer = map.getLayer(map.layerIds[i]);
var url:String;
if (layer.visible)
{
if (layer is ArcGISDynamicMapServiceLayer)
{
var dynamicLayer:ArcGISDynamicMapServiceLayer = layer as ArcGISDynamicMapServiceLayer;
url = dynamicLayer.url;
}
else if (layer is ArcGISTiledMapServiceLayer)
{
var tiledLayer:ArcGISTiledMapServiceLayer = layer as ArcGISTiledMapServiceLayer;
url = tiledLayer.url;
}
if(url)
{
var identifyTask:IdentifyTask = new IdentifyTask(url);
identifyTask.addEventListener(IdentifyEvent.IDENTI FY_COMPLETE, onResult);
identifyTask.addEventListener(FaultEvent.FAULT, onFault);
identifyTask.execute(identifyParams);
showMessage(loadingLabel, true);
showStateResults(null);
}
}
}
}
// on result
private function onResult(event:IdentifyEvent):void
{
try
{
processIdentifyResults(event.identifyResults);
}
catch (error:Error)
{
showMessage(error.message, false);
}
}
//on fault
private function onFault(event:FaultEvent):void
{
showMessage(event.fault.faultDetail, false);
}
//proocess identify results
private function processIdentifyResults(identifyResults:Array):void
{
if(!identifyArrayCollection)
identifyArrayCollection = new ArrayCollection();
for each (var identifyResult:IdentifyResult in identifyResults)
{
var title:String = identifyResult.layerName;
var obj:Object = identifyResult.feature.attributes;
var content:String = "";
var fld:String;
var value:String;
for (fld in obj)
{
value = obj[fld].toString();
content += fld + ": " + value + "\n";
}
var infoData:Object =
{
icon: widgetIcon,
title: identifyResult.layerName,
content: content,
link: "",
point: identifyPoint,
geometry: identifyPoint
};
identifyArrayCollection.addItem(infoData);
}
clearMessage();
}
private function clear():void
{
graphicsLayer.clear();
clearMessage();
identifyArrayCollection = null;
identifyPoint = null;
this.showInfoWindow(null);
}
private var hitimer:uint;
private function mouseOverRecord(event:MouseEvent):void
{
var infoData:Object = event.currentTarget.infoData;
clearTimeout(hitimer);
hitimer = setTimeout(showHighlight, 800, [infoData]);
}
private function mouseOutRecord():void
{
clearTimeout(hitimer);
this.showInfoWindow(null);
}
private function clickRecord(event:MouseEvent):void
{
var infoData:Object = event.currentTarget.infoData;
var pt:MapPoint = infoData.point;
if (map.scale > zoomScale)
map.scale = zoomScale;
map.centerAt(pt);
}
private function showHighlight(params:Array):void
{
var infoData:Object = params[0];
var pt:MapPoint = infoData.point;
//if (!map.extent.contains(pt))
// map.centerAt(pt);
this.showInfoWindow(params[0]);
}
private function showMessage(msg:String, swfVisible:Boolean):void
{
txtMessage.text = msg;
swfMessage.visible = swfVisible;
msgVisible = true;
}
private function clearMessage():void
{
msgVisible = false;
}
private function widgetClosedHandler(event:Event):void
{
graphicsLayer.visible = false;
this.showInfoWindow(null);
setMapNavigation(null, null);
}
private function widgetOpenedHandler(event:Event):void
{
activateIdentifyTool();
graphicsLayer.visible = true;
}
private function showStateIdentify(event:MouseEvent):void
{
WidgetEffects.flipWidget(this, viewStack, "selectedIndex", 0, 400);
}
private function showStateResults(event:MouseEvent):void
{
WidgetEffects.flipWidget(this, viewStack, "selectedIndex", 1, 400);
}
]]>
</mx:Script>
[CODE]