map.graphics.clear() and Feature Layer snapshot mode

1329
2
02-10-2014 09:37 PM
vinayb
by
New Contributor III
HI All,


  I am facing issue where my graphic object is getting removed from map .I am using world map and adding feature layer in snap shot mode.
I dont want to hit server again and again so hence i am keeping all featuer layer gaphics in dojo dictionary using below code

 dojo.forEach(featureLayer.graphics, function(graphic){
                hashMap.add(graphic.attributes.CNTRY_NAME, graphic);
            });


now when i want to highlight a country i do below .

if (hashMap.containsKey(country)) {
                                  var graphic = hashMap.item(country);
                                  graphic.setSymbol(alertOfficeSymbol);
                                  map.graphics.add(graphic);
                              }


I also have onClick functionality in my app.

SO lets say onload of page i have to highlight somecountries lets India,USA,UK etc.

now in some scenarios i have to clear these countries i.e i have to deHighlight them so i use below code

if (map.graphics != null) {
        map.graphics.clear();
    }


I have defined my onClick function as below

handle=dojo.connect(map, "onClick", function(e){
              selectCountries(e);
          });

function selectCountries(e){
  var graphic = evt.graphic;
        var countryName = graphic.attributes.CNTRY_NAME;
}


Now when user clicks on India,USA,UK i get below error
evt.graphic is undefined

so my graphic object is lost when i use map.graphics.clear();

how to overcome this.
0 Kudos
2 Replies
JamesVillanueva
New Contributor III
I have run into something similar to this before. In Javascript anything more complicated than a string is an object in memory, and what you pass around is a pointer to that object in memory. Instead of putting the actual graphic in the map container, you should try cloning it:

   map.graphics.add(new Graphic(graphic.toJson()));
0 Kudos
TyroneLigon
Occasional Contributor
In your code snippet for your "selectCountries" function you have "e" as your event input, but "evt.graphic" as the initialization value for "graphic". Is that a typo? Otherwise, "evt.graphic" will always be undefined and you should replace it with "e.graphic".
0 Kudos