See also
Input JSON should match this REST response syntax
{
"displayFieldName" : "description",
"fieldAliases" : {
"objectid" : "Object ID",
"type" : "Type",
"description" : "Description"
},
"geometryType" : "esriGeometryPoint",
"spatialReference" : {
"wkid" : 4326
},
"fields" : [
{
"name" : "objectid",
"type" : "esriFieldTypeOID",
"alias" : "Object ID"
},
{
"name" : "type",
"type" : "esriFieldTypeInteger",
"alias" : "Type"
},
{
"name" : "description",
"type" : "esriFieldTypeString",
"alias" : "Description",
"length" : 1073741822
}
],
"features" : [
{
"attributes" : {
"objectid" : 2010,
"type" : 3,
"description" : "xzc"
},
"geometry" : {
"x" : -117.53253785200002,
"y" : 34.244472716000075
}
},
{
"attributes" : {
"objectid" : 2011,
"type" : 0,
"description" : "adf"
},
"geometry" : {
"x" : -100.62294522999991,
"y" : -15.344261442999937
}
}
]
}<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" xmlns:esri="http://www.esri.com/2008/ags">
<s:layout>
<s:VerticalLayout gap="10"
paddingBottom="10"
paddingLeft="10"
paddingRight="10"
paddingTop="10" />
</s:layout>
<fx:Script>
<![CDATA[
import com.esri.ags.FeatureSet;
import com.esri.ags.Graphic;
import com.esri.ags.geometry.Geometry;
import com.esri.ags.symbols.SimpleFillSymbol;
import com.esri.ags.symbols.SimpleLineSymbol;
import com.esri.ags.symbols.SimpleMarkerSymbol;
import com.esri.ags.utils.JSON;
import com.esri.ags.utils.WebMercatorUtil;
import mx.charts.CategoryAxis;
import mx.controls.Alert;
private const sms:SimpleMarkerSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_TRIANGLE, 22, 0x00FF00);
private var sls:SimpleLineSymbol = new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, 0xFF0000);
private var sfs:SimpleFillSymbol = new SimpleFillSymbol(SimpleFillSymbol.STYLE_DIAGONAL_CROSS, 0x0000FF);
protected function onAddButtonClick(event:MouseEvent):void
{
var jsonString:String = txtInput.text;
if (jsonString.length > 0)
{
try
{
if (rbtnFeatureSetType.selected)
{
var featureSet:FeatureSet = FeatureSet.fromJSON(txtInput.text);
if (featureSet && featureSet.features)
{
addGraphicsToMap(featureSet.features);
}
}
else
{
var jsonObj:* = com.esri.ags.utils.JSON.decode(jsonString);
Alert.show("JSON string is valid", "Message");
// TODO: complete code
}
}
catch (error:Error)
{
trace(error.getStackTrace());
Alert.show("The input string cannot be parsed", "Error");
}
}
else
{
Alert.show("You must input valid json string in text area", "Error");
}
}
private function addGraphicsToMap(graphics:Array):void
{
if (graphicsLayer)
{
var isModified:Boolean = false;
for (var i:int; i < graphics.length; i++)
{
var graphic:Graphic = graphics;
if (graphic && graphic.geometry)
{
var geometry:Geometry = graphic.geometry;
switch (geometry.type)
{
case Geometry.MAPPOINT:
{
graphic.symbol = sms;
break;
}
case Geometry.POLYLINE:
{
graphic.symbol = sls;
break;
}
case Geometry.POLYGON:
{
graphic.symbol = sfs;
break;
}
}
trace("Added graphic id=" + graphicsLayer.add(graphic));
isModified = true;
}
}
if (isModified)
{
graphicsLayer.refresh();
}
}
}
]]>
</fx:Script>
<fx:Script>
<![CDATA[
protected function onClearButtonClick(event:MouseEvent):void
{
if (graphicsLayer)
{
graphicsLayer.clear();
}
}
]]>
</fx:Script>
<fx:Declarations>
<s:RadioButtonGroup id="rbtnGroup"/>
</fx:Declarations>
<s:Panel title="Map panel"
width="100%"
height="100%">
<s:layout>
<s:VerticalLayout gap="5"
verticalAlign="middle"
horizontalAlign="center" />
</s:layout>
<s:Button label="Clear graphics"
click="onClearButtonClick(event)" />
<esri:Map id="map">
<esri:ArcGISTiledMapServiceLayer id="baseLayer"
url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_Imagery_World_2D/MapServer"
alpha="0.7" />
<esri:GraphicsLayer id="graphicsLayer" />
</esri:Map>
</s:Panel>
<s:Panel title="Input panel"
width="100%"
height="100%">
<s:layout>
<s:VerticalLayout gap="5"
verticalAlign="middle"
horizontalAlign="center"
paddingBottom="5"/>
</s:layout>
<s:TextArea id="txtInput"
width="100%"
height="100%" />
<s:HGroup width="100%"
horizontalAlign="center"
gap="20">
<s:RadioButton id="rbtnFeatureSetType"
label="is FeatureSet"
selected="true"
group="{rbtnGroup}"/>
<s:RadioButton id="rbtnCustomType"
label="is not FeatureSet"
group="{rbtnGroup}" />
<s:Button label="Add features"
click="onAddButtonClick(event)" />
</s:HGroup>
</s:Panel>
</s:Application>