JSON to Graphic to Map

2286
3
Jump to solution
04-17-2014 01:39 PM
NathanDorman
New Contributor II
Hey guys,
I was wondering if the documentation on website API is up to date or if I'm just missing something. I'm trying to add a graphic to my map by creating a graphic using JSON by the format and parameters stated in the API.

validation checked w/ jsonlint.com
var json = {"geometry":{"points":[[608647.8,2368271.8],[608252.7,2368339.2],[600298.56,2384759.5],[598867.1,2384862],[597231.44,2384743.2],[599478.1,2384728.8],[598811.1,2384355.5],[599798.75,2384646.2],[600041.44,2384662.5],[598171.25,2389613.5],[598431.3,2389301],[595302.94,2388870],[600176.1,2375361.8],[599342.7,2382036.5],[594301.56,2382029.8],[603859.9,2381974],[594440.3,2382028.8],[602758.9,2381808.2],[594808.5,2381696.8],[599076.9,2377178.8]],"spatialReference":{"wkid":26904,"latestWkid":26904}},"symbol":{"color":[120,255,120,64],"size":20,"angle":0,"xoffset":0,"yoffset":0,"type":"esriSMS","style":"esriSMSX","outline":{"color":[0,0,0,255],"width":5,"type":"esriSLS","style":"esriSLSSolid"}}}


var graphic = new Graphic(json);
// This doesn't work
map.getLayers("graphicsLayer").add(graphic);

/* this also doesn't work
map.graphics.add(graphic);
*/
// I also call graphic.toJson() and console.log(map) to check their validity

When I inspect the HTML, I do see that the graphics layer element under "#content_layers > svg >" g does exist and just contains:
<g id="series_layer" transform="matrix(1.00000000,0.00000000,0.00000000,1.00000000,25.00000000,85.00000000)" style="display: block;"><g></g></g>

I thought it might be an ordering issue, but I made my only dynamic map layer transparent, which had no effect.

I also checked to make sure that map was not undefined, and I'm sure that all my references do exist.

I'm on JS API v3.8 and did try 3.9.

Any suggestions? I feel like I'm close, but can't figure it out.

Thanks,
0 Kudos
1 Solution

Accepted Solutions
JonathanUihlein
Esri Regular Contributor
You are close!

Here's a sample using different points, but the same symbol.

http://jsfiddle.net/tE4A3/8/

If your graphic still doesn't draw, you may end up having to convert those points to wkid 102100 or 4326.

View solution in original post

0 Kudos
3 Replies
JonathanUihlein
Esri Regular Contributor
You are close!

Here's a sample using different points, but the same symbol.

http://jsfiddle.net/tE4A3/8/

If your graphic still doesn't draw, you may end up having to convert those points to wkid 102100 or 4326.
0 Kudos
NathanDorman
New Contributor II
Thanks for the reply. It's good to see some working sample of a graphic created by JSON. But your point about projection might be the solution to my problem. I still can't seem to render the graphic.

will calling
point.setSaptialReference({ "wkid": 4326 });
resolve the spatial reference issue and update my x and y coordinates?
0 Kudos
NathanDorman
New Contributor II
Thanks Jon, I ended up having to change the spatialreference of all my data (I was thinking of reprojecting using JS).
0 Kudos