I have a graphics layer that has a graphics length of 2 because I added 3 graphics. When I try to reference the graphicLayer.graphics[0] the system is telling me that it is undefined. If I have 3 graphics why would it be undefined?
David,
In the 4.x API the graphics property return a Collection not a flat array like the 3.x API did. SO you have to use collection methods.
<SPAN class="comment token">//Old way in 3.x</SPAN> graphicLayer<SPAN class="punctuation token">.</SPAN>graphics<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">//New way in 4.x</SPAN> graphicLayer<SPAN class="punctuation token">.</SPAN>graphics<SPAN class="punctuation token">.</SPAN><SPAN class="token function">getItemAt</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Thank you and sorry about the javascript error. It is working now.
So you specify gL as your interator index and then you use gl (all lower case) this is an issue in JavaScript as it is case sensitive
I am actually comparing it in a for loop to the 'geography' which is a variable. The javascript blows up on the compare. In the immediate window, I try to see what the value is
? highlightGeographyLyr.graphics.getItemAt(gl).attributes[0]
It gives me a value of {...} in the immediate window so something seems to be there.
for (gL = 0; highlightGeographyLyr.graphics.length - 1; gL++) { if (highlightGeographyLyr.graphics.getItemAt(gl).attributes[0] == geography) { highlightGeographyLyr.remove(highlightGeographyLyr.graphics.getItemAt(gl)); }}
Strange a Collection is a zero based index and the attributes are an Object so your first line should work.
For 1 graphic in the layer, the below lines error and say it is undefined.
highlightGeographyLyr.graphics.getItemAt(0).attributes[0]
highlightGeographyLyr.graphics.getItemAt(0).attributes(0)
highlightGeographyLyr.graphics.getItemAt(0).attributes.id
Would it not be length - 1 in your for loop?
<SPAN class="keyword token">for</SPAN> <SPAN class="punctuation token">(</SPAN>gL <SPAN class="operator token">=</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">;</SPAN> highlightGeographyLyr<SPAN class="punctuation token">.</SPAN>graphics<SPAN class="punctuation token">.</SPAN>length <SPAN class="operator token">-</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">;</SPAN> gL<SPAN class="operator token">++</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN></SPAN>
The graphicLayer.graphics.getItemAt(0) works to get the graphic. Thank you. One last issue though. I am creating the graphic and adding it to the layer using the below code in bold. At the end I add 'id' as an attribute to uniquely identify it.
In another process, directly below, I loop through the graphic layer looking for a graphic with an 'id' value. I am getting errors when trying to access the attributes section.
for (gL = 0; highlightGeographyLyr.graphics.length; gL++) { if (highlightGeographyLyr.graphics.getItemAt(gl).attributes[0] == geography) { highlightGeographyLyr.remove(highlightGeographyLyr.graphics.getItemAt(gl));} }
function highLightGeography(response) {
// Loop through each of the results and assign a symbol and PopupTemplate // to each so they may be visualized on the map var geography = ""; var peakResults = arrayUtils.map(response.features, function ( feature) {
geography = feature.attributes.ID_; // Sets the symbol of each resulting feature to a cone with a // fixed color and width. The height is based on the mountain's elevation feature.symbol = { type: "simple-fill", // autocasts as new PointSymbol3D() color: { r: 245, g: 245, b: 245, a: 0.5 }, style: "solid", opacity: 0.2, outline: { color: "white", width: 1 } };
return feature; });
peakResults.attributes = { "id": geography }; highlightGeographyLyr.addMany(peakResults); }
Can you post the code showing how you define graphicsLayer and add the graphics to it?
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.