Select to view content in your preferred language

How to pass an ArcGIS object via JSON

1231
5
03-26-2012 09:32 PM
StephenLead
Honored Contributor
The Create Web Map from JSON example shows how to pass parameters from a configuration file to the map:

webmap.itemData ={
          "operationalLayers":[{
            "url":"http://server.arcgisonline.com/ArcGIS/rest/services/Specialty/Soil_Survey_Map/MapServer",
            "visibility":true,
            "opacity":0.75,
            "title":"Soil Survey Map",
            "itemId":"204d94c9b1374de9a21574c9efa31164"
          }]
var layers = response.itemInfo.itemData.operationalLayers;


All of these items are either text, numbers, or boolean. How can I pass an ArcGIS object via JSON? For example, I want to specify a symbol using the same syntax as above. How would I include this symbol in the JSON?

new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID,
 new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_DASHDOT,
 new dojo.Color([255,0,0]), 2),new dojo.Color([255,255,0,0.25]));


If I include this in the JSON without quotation marks, it's marked as an error since the JSON file doesn't know what esri.symbol.SimpleFillSymbol means. If I quote it, ArcGIS doesn't recognise it as an object since it's now a string.

Do I need to break it into the component parts (STYLE_SOLID, STYLE_DASHDOT, [255,0,0], [255,255,0,0.25]) then reassemble?

Thanks for any tips,
Steve
0 Kudos
5 Replies
nicogis
MVP Alum
Are you using toJson() ?  (mySymbol.toJson())
0 Kudos
StephenLead
Honored Contributor
Are you using toJson() ?  (mySymbol.toJson())


Would that work for any property? eg mode: esri.layers.FeatureLayer.MODE_SELECTION?

Basically, I'm trying to extend the
itemData

object in the example above, for example to specify the feature layer mode and other properties.
Thanks,
Steve

0 Kudos
nicogis
MVP Alum
when you, for example, write a property esri.symbol.SimpleLineSymbol.style (enumerator)
( underlying you are writing in this sample a string)
for be understood from code must have access at this definition.
 var cls = new esri.symbol.CartographicLineSymbol(esri.symbol.CartographicLineSymbol.STYLE_SOLID,
      new dojo.Color([255,0,0]), 10, esri.symbol.CartographicLineSymbol.CAP_ROUND,
      esri.symbol.CartographicLineSymbol.JOIN_MITER, 5);
    console.debug(cls.toJson());



here you can see the result (example style)
style: "esriSLSSolid"

esriSLSSolid is the constant string in enumerator arcobject for style esriSimpleLineStyle http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/001w/001w0000003m000000.htm
0 Kudos
StephenLead
Honored Contributor
Thanks Domenico, I'm getting closer. But still not working 100% yet.

I've modified one of the Esri samples to deliver the symbology via the JSON file:

itemData ={
         "symbols":[
         {
          "fillStyle": 'esriSFSSolid',
          "lineStyle": 'esriSLSSolid',
          "lineWeight": 3,
          "lineColor": [255, 0, 0],
          "fillColor": [125, 125, 125, 0.35]
         }
         ]
        };


var fillStyle = itemData.symbols[0].fillStyle;
var lineStyle = itemData.symbols[0].lineStyle;
var lineWeight = itemData.symbols[0].lineWeight;
var lineColor = itemData.symbols[0].lineColor;
var fillColor = itemData.symbols[0].fillColor;
var highlightSymbol = new esri.symbol.SimpleFillSymbol(fillStyle, new esri.symbol.SimpleLineSymbol(lineStyle, new dojo.Color(lineColor), lineWeight), new dojo.Color(fillColor));


This is almost working, but isn't quite right. Any further tips?

Thanks,
Steve
0 Kudos
nicogis
MVP Alum
Have you tried esri.symbol.fromJson() ?

var duplicate = esri.symbol.fromJson(simpleMarkerSymbol.toJson());
0 Kudos