matching id array to graphic.attributes

552
3
Jump to solution
01-25-2012 11:09 AM
JanicePeal
Occasional Contributor
If I have an array that contains, say, 5 items all with an attribute called ObjectID and I want to check this array against the attributes of graphics loaded into a graphics layer as map points, how do I loop through those graphics' attributes?

for (var i2:Number = 0; i2 < oidArray.length; i2++)     {      try      {       resultsoid = ""+oidArray[i2].ObjectID;       Alert.show(resultsoid+"\n\n"+""+allGraphicMarkers.attributes, "test wth");       for each (var attributes:Object in allGraphicMarkers.attributes)       {        if (resultsoid == (""+allGraphicMarkers.attributes))        {                  //highlightedGraphicMarker.symbol = _sms_resultsBlue;         Alert.show(resultsoid+"\n\n"+""+allGraphicMarkers.attributes, "did i get here2?");         break;        }                }      }      catch(errorObj:Error)      {       Alert.show(errorObj.toString(),"1361");      }


I know my problem lies in the for each area and quite possibly how I'm matching the graphic attributes in the if statement. Eventually on a match, I'd like the marker symbol to change to blue (temporarily commented out). I've been fighting with this all day.  Any assistance would be greatly appreciated.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus
Janice,

     You are trying to compare a string to and array of attributes.

It should look something more like this:

                                for (var i2:Number = 0; i2 < oidArray.length; i2++)                                 {                                     try                                     {                                         var obj:Object = allGraphicMarkers.attributes;                                         //Assuming the ObjectID is the name of the object id field in the layer that was queried                                         if ( obj["ObjectID"] == oidArray[i2].ObjectID)                                             Alert.show(oidArray[i2].ObjectID.toString() + "\n\n" + obj["ObjectID"].toString(), "did i get here2?");                                     }                                     catch(errorObj:Error)                                     {                                         Alert.show(errorObj.toString(),"1361");                                     }                                 }



Don't forget to click the top arrow (promote) and to click the Mark as answer check as shown below:

View solution in original post

0 Kudos
3 Replies
RobertScheitlin__GISP
MVP Emeritus
Janice,

     You are trying to compare a string to and array of attributes.

It should look something more like this:

                                for (var i2:Number = 0; i2 < oidArray.length; i2++)                                 {                                     try                                     {                                         var obj:Object = allGraphicMarkers.attributes;                                         //Assuming the ObjectID is the name of the object id field in the layer that was queried                                         if ( obj["ObjectID"] == oidArray[i2].ObjectID)                                             Alert.show(oidArray[i2].ObjectID.toString() + "\n\n" + obj["ObjectID"].toString(), "did i get here2?");                                     }                                     catch(errorObj:Error)                                     {                                         Alert.show(errorObj.toString(),"1361");                                     }                                 }



Don't forget to click the top arrow (promote) and to click the Mark as answer check as shown below:
0 Kudos
JanicePeal
Occasional Contributor
Thank you for your feedback.  I may have overwritten some code in extreme frustration.  Good catch.

I think perhaps my problem lies in my graphics attributes themselves, and although I can see the values of the ObjectID, i may not have assigned the attributes correctly and that's why I can't loop through them. 

I have an array (parsedXYArrayNEW) that contains "X", "Y", and "ObjectID".  I am using this array to generate graphics:

 
for (var i3:int = 0; i3 < parsedXYArrayNEW.length; i3++)
{
 var myResult2:MapPoint = new MapPoint(Number(parsedXYArrayNEW[i3].X),Number(parsedXYArrayNEW[i3].Y),new SpatialReference(3424)); 
 allGraphicMarkers = new Graphic(myResult2,_sms_resultsRed_AP);
 allGraphicMarkers.attributes = parsedXYArrayNEW[i3].ObjectID;//if I didn't add this, I wouldn't get any attributes in my graphics.
 Alert.show(""+allGraphicMarkers.attributes,"1263");//yes, this returns a distinct ObjectID value (the only thing populated in the alert box 
                                                                           //is a numeric value like 111111 or 111112) 
                                                                           //for each graphic, but is it really "ObjectID"?
 this._gl_resultsMarkersAll.add(allGraphicMarkers);
  
}
}

Then I want to compare a selection set of ObjectIDs from a list (storing it as "ObjectID" in an array called oidArray) to the graphics generated in the step above and highlight the graphics that match with a different color.  
var obj:Object = allGraphicMarkers.attributes;
for (var i2:Number = 0; i2 < oidArray.length; i2++)
{
//your suggested code which makes total sense    
if ( obj["ObjectID"] == oidArray[i2].ObjectID)
   {
 Alert.show(oidArray[i2].ObjectID.toString() + "\n\n" + obj["ObjectID"].toString(), "did i get here2?");
       //allGraphicsMarkers.symbol = _sms_resultsBlue;
    }
}


Sadly, this has returned a ReferenceError: Error #1069 (aka - Property Children not found) which means that I totally messed up getting the ObjectID attribute into the allGraphicsMarkers graphic in the first place.  So how should I be generating the graphics from the xy coordinates and properly assigning the attribute ObjectID and it's value for each?
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Janice,

  Try this for the first bit of code:

for (var i3:int = 0; i3 < parsedXYArrayNEW.length; i3++)
{
    var myResult2:MapPoint = new MapPoint(Number(parsedXYArrayNEW[i3].X),Number(parsedXYArrayNEW[i3].Y),new SpatialReference(3424)); 
    allGraphicMarkers = new Graphic(myResult2,_sms_resultsRed_AP);
    var atts:Object = new Object();
    atts.ObjectID = Number(parsedXYArrayNEW[i3].ObjectID);
    atts.xCoord = Number(parsedXYArrayNEW[i3].X);
    atts.yCoord = Number(parsedXYArrayNEW[i3].Y);
    allGraphicMarkers.attributes = atts;
    Alert.show(allGraphicMarkers.attributes.ObjectID.toString(),"1263");
    _gl_resultsMarkersAll.add(allGraphicMarkers);
}
0 Kudos