Select to view content in your preferred language

Trouble display graphics from a GraphicsLayer

965
3
Jump to solution
09-20-2013 01:11 PM
SteveCole
Honored Contributor
I have an application that I'm trying to extend by adding the ability for my users to add a graphic point, line, or polygon and then  use that graphic as part of some analysis that I've developed. I'm using this sample's code as the basis for my code to add a user drawn graphic. This all worked fine when it was just adding the graphic to the default map graphics layer.

Because I wanted to have popups for the graphics that a user adds, I decided that I wanted to create a graphicsLayer to house any user drawn graphics. I've defined the graphicsLayer this way:

 var graphicsTemplate = new esri.InfoTemplate();  graphicsTemplate.setTitle('Custom Project');  graphicsTemplate.setContent(graphicsLayerContent);   //Define the Graphics Layer which contains the user drawn "projects"  theGLayer = new esri.layers.GraphicsLayer({   id: "gLayer",   opacity:0.80,   visible:true  });   theGLayer.setInfoTemplate(graphicsTemplate);   map.addLayer(theGLayer);


Within my application, I've set things up to basically operate like the ESRI sample I linked above. My function to add the graphic that the user draws is the following:

function addGraphic(evt) {  //deactivate the toolbar and clear existing graphics   tb.deactivate();   map.enableMapNavigation();  theGLayer.clear(); //map.graphics.clear();    // figure out which symbol to use  var symbol, g;  if ( evt.type === "point" || evt.type === "multipoint") {   symbol = markerSymbol;  } else if ( evt.type === "line" || evt.type === "polyline") {   symbol = lineSymbol;  }  else {   symbol = fillSymbol;  }    attributes = {"ProjTitle": dojo.attr("intPrjName","value"),"projLimits": dojo.attr("intPrjLimits","value")};  g = new esri.Graphic(evt, symbol, attributes, null);    theGLayer.add(new esri.Graphic(g)); //map.graphics.add(new esri.Graphic(evt, symbol)); }


My code throws no errors and adds a graphic to the graphicsLayer (theGLayer.graphics.length =1) but it isn't visible. If I add a point, the X,Y coordinates of the stored graphic point match the X,Y coordinates of the evt object and the spatialReference of the graphic matches the spatialReference of the map. Everything tells me that the layer is visible but I don't see my graphics when I add them.

What am I missing??

Thanks!
Steve
0 Kudos
1 Solution

Accepted Solutions
JasonZou
Frequent Contributor
function addGraphic(evt)

Make sure evt here is a graphic object, not the event object returned from drawToolbar.onDrawEnd event.

Change:
theGLayer.add(new esri.Graphic(g)); //map.graphics.add(new esri.Graphic(evt, symbol));


To:
theGLayer.add(g);

g is already a graphic.

View solution in original post

0 Kudos
3 Replies
JasonZou
Frequent Contributor
function addGraphic(evt)

Make sure evt here is a graphic object, not the event object returned from drawToolbar.onDrawEnd event.

Change:
theGLayer.add(new esri.Graphic(g)); //map.graphics.add(new esri.Graphic(evt, symbol));


To:
theGLayer.add(g);

g is already a graphic.
0 Kudos
SteveCole
Honored Contributor
FINALLY!

Thanks, Jason for catching that. After making the change, everything works like I want it to.

Sometimes it certainly pays to have another set of eyes! Thanks again.
0 Kudos
JasonZou
Frequent Contributor
Glad to help:)
0 Kudos