Reputation: 53
If you reference the image, JSON string, and code snippets below, it outlines the code, console output and expected behavior of trying to draw a polygon (or really any geometry/graphic) using the ESRI ArcGIS Javascript API. Not sure what is going on....help please!
JSON String:
{"geometry":{"rings":[[[-91.89013671874848,38.03029444608522],[-91.653930664061,38.00865683368494],[-91.64843749999851,38.00432854459864],[-91.5935058593735,37.93070854451552],[-91.577026367186,37.88303274722063],[-91.577026367186,37.79192956603227],[-91.631958007811,37.73982010276601],[-91.70886230468598,37.73547599031287],[-91.763793945311,37.76587942393493],[-91.85168457031098,37.85701474874939],[-91.88464355468598,37.9956711998967],[-91.89013671874848,38.03029444608522]]],"spatialReference":{"wkid":4326}},"symbol":{"color":[0,0,0,64],"outline":{"color":[0,0,0,255],"width":1,"type":"esriSLS","style":"esriSLSSolid"},"type":"esriSFS","style":"esriSFSSolid"}}
Code to add Shape to Map:
function createFromJSON(JSONText){
console.log("In Create Function");
dojo.disconnect(handle);
var jsontext = JSON.parse(JSONText);
var polygon = new esri.geometry.Polygon(jsontext);
console.log("Here is the polygon object:");
console.log(polygon);
console.log("Now drawing polygon");
map.graphics.add(new Graphic(polygon, new SimpleFillSymbol()));
console.log("Polygon should be there");
}
Upvotes: 3
Views: 1670
Reputation: 32511
As T Kambi said, your string is for graphic not geometry, but I'm going to show some ideas in converting between json and esriGeometry.
You can convert json to esri.Geometry
, using any of these ways:
JsonUtils
(esri/geometry/jsonUtils
) or esri.geometry.fromJson
method. Here is the code:
METHOD ONE (USING JsonUtils
)
require(
["esri/map", "esri/geometry/jsonUtils", "esri/config", "dojo/domReady!"],
function (Map, JsonUtils, esriConfig) {
var jsonGeometry = {"x":10,"y":20,"spatialReference":{"wkid":3857}};
//Note: you should not use JsonUtils.fromJson(JSON.stringify(jsonGeometry))
var geometry = JsonUtils.fromJson(jsonGeometry);
var graphic = new esri.Graphic(firstGeometry);
});
METHOD TWO (Using geometry.fromJson
method)
var jsonGeometry = {"x":10,"y":20,"spatialReference":{"wkid":3857}};
var geometry = esri.geometry.fromJson(jsonGeometry);
var graphic = new esri.Graphic(geometry);
Upvotes: 0
Reputation: 1387
The JSON string shown in the image is for Graphic
object and not a geometry. As you can see it contains geometry
& symbol
, If you pass it to Graphic it will work.
map.graphics.add(new Graphic(jsontext));
Or if you just want the polygon then your code should be something like this.
var polygon = new esri.geometry.Polygon(jsontext.geometry);
Also, dont combine legacy and AMD style together.
Upvotes: 4