GraphicsLayer issues

2610
13
09-23-2011 12:03 PM
SamirGambhir
Occasional Contributor III
Hello,
I am developing an application using ArcGIS Server 10 with JavaScript API. I am running into some issues with displaying GraphicsLayer on the map. I am trying to add a GraphicsLayer based on a query to an existing layer. The query adds the GraphicsLayer on the map and renders it based on the renderer assigned. Once displayed, clicking a features open up an infoWindow to display the query results. I have the following three issues:
1. Once I select the indicator, the layer should show up. But in my case it shows up only when I zoom in or out of the current extent.
2. Option "None" does not remove the layer from the map
3. Option "All" does not show up all the three indicators.
My code is attached as a text file. I'll really appreciate it if somebody can help.
Thanks
Samir
0 Kudos
13 Replies
derekswingley1
Frequent Contributor
I think you should look into using a feature layer as it can (probably) simplify your code as it can handle a lot of this work for you.

Regarding your specific points...
1. Does that happen in all browsers? Which version of the API are you using?
2. Use show()/hide() to toggle the visibility of the layer rather than actually removing it from the map.
3. Don't repeatedly add/remove your layer. Use show() and hide(). Set your renderer before calling show().
0 Kudos
SamirGambhir
Occasional Contributor III
Hi Derek,
Could you point me to some examples or two on this issue? I tried simplifying my code using FeatureLayer but it did not work. I went back to GraphicsLayer because it was working for me to begin with. If I can look at an example where I can show and hide a GraphicsLayer, it'll be of great help.
Thanks
Samir
0 Kudos
derekswingley1
Frequent Contributor
Hi Samir,

To show/hide a graphicsLayer, just call the layer's show() or hide() method(which is inherited from esri.layers.Layer). The same applies for a feaureLayer.
0 Kudos
SamirGambhir
Occasional Contributor III
Thanks for your help Derek,
It seems like a very simple issue but I am not being able to resolve it. The code below has a problem and I cannot figure it out. Can you please help me iron this out?

function showQueryResults(queryTask1, query1, Indicator){
   
dojo.connect(queryTask1, "onComplete", function(featureSet) {
     distGraphicsLayer = new esri.layers.GraphicsLayer();
       for (var i=0, il=featureSet.features.length; i<il; i++) {
              var graphic = featureSet.features;
              graphic.setInfoTemplate(infoTemplate);
     distGraphicsLayer.add(graphic);
      }  //for
       
    map.addLayer(distGraphicsLayer);
    distGraphicsLayer.setRenderer(renderer);
   
    for (j=0; j<distIndicators.length; j++){
    
     if (distIndicators==Indicator){
       distGraphicsLayer.show();
     }
     else {
      distGraphicsLayer.hide();
     }
    }

Thanks
Samir
0 Kudos
HemingZhu
Occasional Contributor III
Thanks for your help Derek,
It seems like a very simple issue but I am not being able to resolve it. The code below has a problem and I cannot figure it out. Can you please help me iron this out?

function showQueryResults(queryTask1, query1, Indicator){
   
dojo.connect(queryTask1, "onComplete", function(featureSet) {
     distGraphicsLayer = new esri.layers.GraphicsLayer();
       for (var i=0, il=featureSet.features.length; i<il; i++) {
              var graphic = featureSet.features;
              graphic.setInfoTemplate(infoTemplate);
     distGraphicsLayer.add(graphic);
      }  //for
       
    map.addLayer(distGraphicsLayer);
    distGraphicsLayer.setRenderer(renderer);
   
    for (j=0; j<distIndicators.length; j++){
    
     if (distIndicators==Indicator){
       distGraphicsLayer.show();
     }
     else {
      distGraphicsLayer.hide();
     }
    }

Thanks
Samir


A couple of issues. First where is the queryTask1.execute statement ? Secondly everytime you run the queryTask, it will create new graphic layer and add to the map. It is unnecessary. You could just clear the graphics and add the graphcs everytime you run the querytask. By the way dojo.connect(queryTask1, "onComplete", function(featureSet) needs to put outside the function showQueryResults.
0 Kudos
SamirGambhir
Occasional Contributor III
Hi Heming,
Thanks for your response. I am attaching the entire code for this function which'll give you a complete picture. I understand your point about the code creating new graphics layer each time. That might be the reason that each time I change my indicator, the new graphics layer shows on top of the previous one. I'll appreciate it if you can look at this code and suggest a solution.
Thanks
Samir
0 Kudos
HemingZhu
Occasional Contributor III
Hi Heming,
Thanks for your response. I am attaching the entire code for this function which'll give you a complete picture. I understand your point about the code creating new graphics layer each time. That might be the reason that each time I change my indicator, the new graphics layer shows on top of the previous one. I'll appreciate it if you can look at this code and suggest a solution.
Thanks
Samir


I don't know the whole context of code or logic flow. so the changes i made on your code is purely my guess based on your attachment. Hope it will help
0 Kudos
SamirGambhir
Occasional Contributor III
Hi Heming,
I tested your code to and made some minor changes based on my flow, but the graphics still do not show up. Can I email you my entire code for you to look at? Would you have time to resolve this issue for me? I have spent a lot of time trying to solve this problem.
Thanks
Samir
0 Kudos
HemingZhu
Occasional Contributor III
Hi Heming,
I tested your code to and made some minor changes based on my flow, but the graphics still do not show up. Can I email you my entire code for you to look at? Would you have time to resolve this issue for me? I have spent a lot of time trying to solve this problem.
Thanks
Samir


Could you attach you code and post here?
0 Kudos