graphics into graphics layer

960
9
04-24-2014 11:55 AM
MattTenold
New Contributor III
I have graphics currently in a map.  How do I take the array of created graphics and put them into a graphicslayer?  I tried inside my jquery for each loop GraphicsLayer.add(graphic) after I set the graphic = mypt and graphic.setSymbol() which is set as a new PictureMarkerSymbol which is a icon, 36, 36.
0 Kudos
9 Replies
JonathanUihlein
Esri Regular Contributor
Aside from a potential syntax error, what error messages do you see in the console?

Could you create a sample using jsfiddle so I can see your progress so far?

Also check out this sample if you haven't already:
https://developers.arcgis.com/javascript/jssamples/graphics_add.html
0 Kudos
MattTenold
New Contributor III
Sorry I can't put it into a js fiddle, its way to complicated.  But currently I am using a jquery for each to take the graphics and push each one into a variable that I defined as a array and then I am pushing the graphics into it.   I am then taking the graphics  array and adding the array to a new esri.layers.GraphicsLayer.   And I am trying to push the graphics layer to the map.  Currently I am getting an error that self._map.add(GraphicsLayer) is undefined.  I have been debugging and I can see self._map is finding the div object map however it doesn't seem to be able find the add function.
0 Kudos
RobertBurke
Esri Contributor
Hi Mathew,

After you create the graphics layer you should add the layer to the map with addLayer

var gl = new GraphicsLayer();

map.addLayer(gl);
0 Kudos
JonathanUihlein
Esri Regular Contributor
Sorry I can't put it into a js fiddle, its way to complicated.  But currently I am using a jquery for each to take the graphics and push each one into a variable that I defined as a array and then I am pushing the graphics into it.   I am then taking the graphics  array and adding the array to a new esri.layers.GraphicsLayer.   And I am trying to push the graphics layer to the map.  Currently I am getting an error that self._map.add(GraphicsLayer) is undefined.  I have been debugging and I can see self._map is finding the div object map however it doesn't seem to be able find the add function.


Matthew,

It's not as complicated as you think 🙂 It just takes some genuine effort.

I was able to create a sample for you, hopefully it helps.

http://jsfiddle.net/ECq4Z/
0 Kudos
MattTenold
New Contributor III
Thank you Jon and Robert for the help and the js fiddle showing how to push the individual graphics into the graphics layer.  Currently my biggest hurdle is that I using a jquery for each to create the esri graphics, and I can't get the array to populate.  Here is my current code.  I am doing this because I am using a knockout view model with events subevents and self._map refers to the map div.

var SubEventpt;
var SubEventsymbol;
var SubEventgraphic;
var SubEventgraphics = new Array;
var subeventsGraphicsLayer = new esri.layers.GraphicsLayer();

$.each(event.Subevents(), function (i, subevent) {
                         SubEventpt = webMercatorUtils.geographicToWebMercator(new Point(subevent.Longitude, subevent.Latitude));
                         SubEventsymbol = new PictureMarkerSymbol('../Images/Incidents/' + subevent.IconPath, 36, 36);
                         SubEventgraphic = self._map.addGraphic(new Graphic(SubEventpt, SubEventsymbol, ko.toJS(subevent)));
                         SubEventgraphic = new Graphic(SubEventpt, SubEventsymbol, ko.toJS(subevent));
                         SubEventgraphics.push[SubEventgraphic];

                    });
                    
                    subeventsGraphicsLayer.add(SubEventgraphics);
                    self._map.add(subeventsGraphicsLayer);
0 Kudos
JonathanUihlein
Esri Regular Contributor
A reference to the map's div won't work. You need a reference to the map object itself.

Also, as posted earlier, use addLayer(), not add().


map.addLayer(subeventsGraphicsLayer);



One more thing: it looks like you are using both legacy and AMD style DOJO in your code.
I would suggest using AMD exclusively.
0 Kudos
MattTenold
New Contributor III
Thats the problem I am working with another persons code which he is still developing.  When I run self._map = map, I get the div id plus ".map"   It looks like this.

[ATTACH=CONFIG]33378[/ATTACH]  The self._map = map is a amd module requiring "esri.map"

Isn't this the map object?   I looked at the map view html and the esri map is a class of the map div.
0 Kudos
JonathanUihlein
Esri Regular Contributor
You had said earlier that it was a reference to the div, but if its not, then you are all set 🙂
0 Kudos
MattTenold
New Contributor III
sweet.  thank you.  can you confirm when you see the screenshot what I see is that truly the reference to the map object inside the div?

I am just not used to how the map object nests inside the html.
0 Kudos