Can't create Graphic using a CartographicLineSymbol json symbol.

2276
7
01-21-2014 02:39 AM
PatrickTruitt
New Contributor
Using 3.8. Creating a drawing tool and I'm storing the "toJson()" results in the database. Everything works fine for points and polygons. I can recreate them by simply calling new.Graphic(json); Here is the issue,??? Line symbols or CartographicLineSymbol symbols will not recreate from json. The graphic object gets created with all the attributes and geometry but symbol = null. Has anybody else experience this? Thanks.
0 Kudos
7 Replies
PatrickTruitt
New Contributor
To add more to this weird issue is the fact that I can get a CartographicLine symbol to create from json only if I create it as a SimpleLineSymbol. But If create a SimpleLineSymbol json object and try to create a simpleLineSymbol from that json, it doesn't work. This has to be a Esri bug.
0 Kudos
JonathanUihlein
Esri Regular Contributor
Are the spatial reference objects being generated correctly?

Can you recreate the issue using http://jsfiddle.net so I can take a look?

Thanks!!
0 Kudos
PatrickTruitt
New Contributor
Here is a sample
http://jsfiddle.net/phoxoey/tcLtq/11/

In the setSymbolType() function, if you switch from creating a Cartographic to creating a SimpleLine,.. it will work but by default, creating a Graphic from a Cartographic json object will not work.

Thanks for look at this.
0 Kudos
JonathanUihlein
Esri Regular Contributor
Hi Patrick!

Took a quick look and it seems your symbol object is getting distorted when using toJson().

You will want to recreate the symbol object in code from your db values.

I would also use a polyline.

Here's your addAnnoLineHandler() function slightly redone:


function addAnnoLineHandler(evt) {
    tb.deactivate();
    map.enableMapNavigation();

    var symbol = new esri.symbol.CartographicLineSymbol(esri.symbol.CartographicLineSymbol.STYLE_SOLID,new dojo.Color("#000000"),2,esri.symbol.CartographicLineSymbol.CAP_ROUND,esri.symbol.CartographicLineSymbol.JOIN_MITER,1);
    
    var baseGraphic = new esri.Graphic( evt, annotationSymbol );
    //map.graphics.add(baseGraphic); // adds graphic directly to map
    
    var baseGraphicJson = baseGraphic.toJson();
    
    var polyline = new esri.geometry.Polyline( baseGraphicJson.geometry ); // note use of geometry property
    console.log( 'polyline', polyline );
    
    var graphicFromDB = new esri.Graphic( polyline, symbol );
    console.log( 'graphicFromDB', graphicFromDB );

    gLayer.add( graphicFromDB );
}


NOTE:

If you end up using:

tb.on("draw-complete", addAnnoLineHandler);


over

dojo.connect(tb, "onDrawEnd", addAnnoLineHandler);


... you will need to change addAnnoLineHandler() to accommodate, as 'evt' will no longer be what you expect.
0 Kudos
PatrickTruitt
New Contributor
Thanks for the reply. I understand your response but this solution won't work for me. I need to understand why toJson() works for some symbols but not others. More important, why it does't work for line symbols. If toJson works perfectly for point and polygon symbols why would I expect a different outcome for the other remaining symbols with the same toJson function?
0 Kudos
JonathanUihlein
Esri Regular Contributor
Some objects cannot be recreated from json if certain construction params are not successfully saved in the json representation.

You will want to recreate the symbol object in code from your db values.


I believe you wanted to create your symbols from your db info correct?
You needed to change my code slightly. See below:

function addAnnoLineHandler(evt) {
    tb.deactivate();
    map.enableMapNavigation();

    var baseGraphic = new esri.Graphic( evt, annotationSymbol );
    //map.graphics.add(baseGraphic); // adds graphic directly to map
    var baseGraphicJson = baseGraphic.toJson();
    
    var symbol = new esri.symbol.CartographicLineSymbol( baseGraphicJson.symbol );
    var polyline = new esri.geometry.Polyline( baseGraphicJson.geometry );
    
    var graphicFromDB = new esri.Graphic( polyline, symbol );

    gLayer.add( graphicFromDB );
}


This worked for me. Why does this not work for you? Please be as specific as possible. Thanks!
0 Kudos
PatrickTruitt
New Contributor
This is fine within the specific handler for each symbol because I know that the user is creating a point or a line. The problem is when the user opens the application and it fetches the annotations from the database. I didn't want to have to parse each type in order create the correct symbol and geometry separately. I just want to call Graphic(json) where json has all this information already per the API documentation.
0 Kudos