esri add graphic from json to graphicslayer is giving error

270 views Asked by At

hi i am trying to add graphic to graphicslayer. here is the problem.

i get json result from sketchviewmodel create function which is graphic.

i store this in to new jsonobject. then i try to add this graphic into graphicslayer manually. but its giving error.

here is the json

{
"geometry":{
    "spatialReference":{
     "latestWkid":3857,
     "wkid":102100
  },
  "x":243208.09643883476,
  "y":2940285.766420703,
  "z":351.9580905416081
},
"symbol":{
  "type":"point-3d",
  "symbolLayers":[
     {
        "type":"Icon",
        "material":{
           
        },
        "resource":{
           "primitive":"kite"
        },
        "size":15,
        "outline":{
           "color":[
              0,
              0,
              0
           ],
           "size":2.25
        }
     }
  ]
},
  "attributes":{
   
 },
   "popupTemplate":null
}

this code gives error

const [Graphic] = await loadModules(["esri/Graphic"]);
let g =  new Graphic(data); // data which is json i gave.

any adivce ? Thank you.

1

There are 1 answers

1
cabesuon On BEST ANSWER

Assuming you are using toJSON method of Graphic to generate the json object (in your example data). Then the correct way of creating a Graphic from the json object is to use the "reverse" method fromJSON. Something like this should work,

const [Graphic] = await loadModules(["esri/Graphic"]);
// here use fromJSON method to create the graphic
let g =  Graphic.fromJSON(data); // data which is json i gave.

ArcGIS API - Graphic fromJSON