AnsweredAssumed Answered

Get Graphic attributes without getting Graphics themselves

Question asked by jfjohnson on Feb 6, 2016
Latest reply on Feb 9, 2016 by jfjohnson

I have an application where I need to draw many Graphics on a map, and detect clicks on them. To make this process efficient, I maintain a bi-directional mapping of graphic IDs and object IDs, so basically given a graphic ID, I can determine which of my objects is represented by that graphic. And given one of my objects, I can tell which graphic on the map is representing it.

 

This takes some initial setup, when the Graphics are first drawn on the map. Unfortunately, you can't obtain the id of a Graphics object with something like this:

    Graphic myGraphic = new Graphic()

    graphicLayer.addGraphic(myGraphic)

    graphic.getId() (or graphic.getUid())

 

the methods to get ID and UID both return -1 at this point, which I assume has to do with the immutability of Graphics objects. So what I end up having to do is adding attributes to each graphic that contain the object Id, and then do this:

    int[] graphicIds = graphicLayer.getGraphicIds()

    for (int i = 0; i < graphicIds.length; i++){

        Graphic myGraphic = graphicsLayer.getGraphic(graphicIds[i])

        int objectId = myGraphic.getAttributes().get("MY_OBJECT_ID_KEY")

        //create mapping of  graphicId <--> objectId

    }

 

The issue with this is that the graphicsLayer.getGraphic(int id) method takes a long time, usually from 0.1 - 0.15 seconds. Once we get up to only thirty icons that we need to show on the map, the user will be waiting over three seconds, where the application is completely frozen, since this must be on the UI thread. Is there a way, if you have the graphic id, to get the attributes for a graphic without getting the graphic itself? I'm thinking that might speed up this process. Is there another approach I could take on this problem?

Outcomes