Graphic within GraphicLayer is undefined.

432
9
Jump to solution
08-09-2018 07:14 AM
DavidKucharski
New Contributor II

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?

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Esteemed Contributor

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.

//Old way in 3.x
graphicLayer.graphics[0];

//New way in 4.x
graphicLayer.graphics.getItemAt(0);

View solution in original post

9 Replies
KenBuja
MVP Honored Contributor

Can you post the code showing how you define graphicsLayer and add the graphics to it?

0 Kudos
RobertScheitlin__GISP
MVP Esteemed Contributor

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.

//Old way in 3.x
graphicLayer.graphics[0];

//New way in 4.x
graphicLayer.graphics.getItemAt(0);
DavidKucharski
New Contributor II

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);
}

0 Kudos
RobertScheitlin__GISP
MVP Esteemed Contributor

David,

  Would it not be length - 1 in your for loop?

for (gL = 0; highlightGeographyLyr.graphics.length - 1; gL++) {
0 Kudos
DavidKucharski
New Contributor II

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

0 Kudos
RobertScheitlin__GISP
MVP Esteemed Contributor

David,


  Strange a Collection is a zero based index and the attributes are an Object so your first line should work.

0 Kudos
DavidKucharski
New Contributor II

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));
    }
}

0 Kudos
RobertScheitlin__GISP
MVP Esteemed Contributor

David,


  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

0 Kudos
DavidKucharski
New Contributor II

Thank you and sorry about the javascript error. It is working now.

0 Kudos