Select to view content in your preferred language

FeatureLayer issue

3785
3
10-22-2012 07:32 AM
SamirGambhir
Frequent Contributor
Hi all,
I have this small piece of code which should switch between two feature layers (mode set as 'MODE_ONDEMAND') and display one depending on the selection. Once displayed, it should also show the infoWindow. Though it is a small piece of code, I believe I am missing something because of which it keeps giving me errors. Here is the code:
function queryParam(indc, indValue, flag, geog) { //indc=Indicator, indValue=indicator value, flag returns 'true' or 'false' depending on unit of analysis, geog is the selected State of India using a pull down menu
 query = new esri.tasks.Query();
 query.returnGeometry = true;
 query.outFields = ["*"];
 query.where = "1=1";
 var resultContent = "<tr>" + indc + ": <td>${" + indValue + ":formatNumber}</td></tr>";
 var renderer = new esri.renderer.ClassBreaksRenderer(symbol, indValue);
 renderer.addBreak(0.01, 19, symbolSet31);
 renderer.addBreak(19, 27, symbolSet32);
 renderer.addBreak(27, Infinity, symbolSet33);
 if (flag === true) {
  infoTemplate.setTitle("<tr>State: <td>${State_name}</tr></td>");
  stateFeatureLayer.setRenderer(renderer);
  mapM.addLayer(stateFeatureLayer);
 } else if (flag === false) {
  infoTemplate.setTitle("<tr>DISTRICT: <td>${Dist_name}</tr></td><br /><tr>STATE: <td>${State_name}</tr></td>");
  distFeatureLayer.setRenderer(renderer);
  if (geog != "All India") { //if a single State is selected. the code selects districts (sub-State) to display within the selected State
   distFeatureLayer.setDefinitionExpression("State_name= '" + geog + "'");
   distFeatureLayer.selectFeatures(query, esri.layers.FeatureLayer.SELECTION_NEW);
   dojo.connect(distFeatureLayer, "onSelectionComplete", mapM.addLayer(distFeatureLayer));
  } else if (geog == "All India") { //if "All India" is selected using the pull down menu. This displays all districts in the country
   distFeatureLayer.clearSelection();
                        mapM.addLayer(distFeatureLayer);
  }
 }
 infoTemplate.setContent(resultContent);
}


I get the following errors, primarily when flag==false:

  • The first State selected works well but shows a 'type error' in the web console. Changing geog from one state to another displays the features of the next State but does not bring up the infoWindow when clicked on the map. This shows a 'type error' too.

  • Selecting "All India" does not remove the previous selection and does not display all features.

Please see if you can test this code using your data. I'll be happy to provide the underlying html document.

Thanks
Samir
0 Kudos
3 Replies
SamirGambhir
Frequent Contributor
Hi,
Please look at this code and help me resolve this issue. I have spent a lot of time on this with no results.
Thanks
Samir
Hi all,
I have this small piece of code which should switch between two feature layers (mode set as 'MODE_ONDEMAND') and display one depending on the selection. Once displayed, it should also show the infoWindow. Though it is a small piece of code, I believe I am missing something because of which it keeps giving me errors. Here is the code:
function queryParam(indc, indValue, flag, geog) { //indc=Indicator, indValue=indicator value, flag returns 'true' or 'false' depending on unit of analysis, geog is the selected State of India using a pull down menu
 query = new esri.tasks.Query();
 query.returnGeometry = true;
 query.outFields = ["*"];
 query.where = "1=1";
 var resultContent = "<tr>" + indc + ": <td>${" + indValue + ":formatNumber}</td></tr>";
 var renderer = new esri.renderer.ClassBreaksRenderer(symbol, indValue);
 renderer.addBreak(0.01, 19, symbolSet31);
 renderer.addBreak(19, 27, symbolSet32);
 renderer.addBreak(27, Infinity, symbolSet33);
 if (flag === true) {
  infoTemplate.setTitle("<tr>State: <td>${State_name}</tr></td>");
  stateFeatureLayer.setRenderer(renderer);
  mapM.addLayer(stateFeatureLayer);
 } else if (flag === false) {
  infoTemplate.setTitle("<tr>DISTRICT: <td>${Dist_name}</tr></td><br /><tr>STATE: <td>${State_name}</tr></td>");
  distFeatureLayer.setRenderer(renderer);
  if (geog != "All India") { //if a single State is selected. the code selects districts (sub-State) to display within the selected State
   distFeatureLayer.setDefinitionExpression("State_name= '" + geog + "'");
   distFeatureLayer.selectFeatures(query, esri.layers.FeatureLayer.SELECTION_NEW);
   dojo.connect(distFeatureLayer, "onSelectionComplete", mapM.addLayer(distFeatureLayer));
  } else if (geog == "All India") { //if "All India" is selected using the pull down menu. This displays all districts in the country
   distFeatureLayer.clearSelection();
                        mapM.addLayer(distFeatureLayer);
  }
 }
 infoTemplate.setContent(resultContent);
}


I get the following errors, primarily when flag==false:

  • The first State selected works well but shows a 'type error' in the web console. Changing geog from one state to another displays the features of the next State but does not bring up the infoWindow when clicked on the map. This shows a 'type error' too.

  • Selecting "All India" does not remove the previous selection and does not display all features.


Please see if you can test this code using your data. I'll be happy to provide the underlying html document.

Thanks
Samir
0 Kudos
SamirGambhir
Frequent Contributor
Hi,
I resolved the second issue by setting the definition expression to 'null' when "geog=='All India'": distFeatureLayer.setDefinitionExpression(null);
However, a new issue has cropped up: infoWindow works at the very first selection. Correct legend shows up as well at the first selection. Any subsequent selection does not bring up both. Also, for any subsequent selection of a different indicator value, anuncaught error shows up:"Uncaught TypeError: Cannot read property 'firstChild' of null". I have checked my code and I do not find any reference to firstChild in my code. Additionally, switching between State and District brings up another uncaught error: "Uncaught TypeError: Cannot call method '_decRefCount' of null". I am unable to make sense of all this. Has anybody faced a similar situation? I'll be happy to share my code if someone might want to look at this issue.
Thanks
Samir
Hi, 
Please look at this code and help me resolve this issue. I have spent a lot of time on this with no results.  
Thanks 
Samir
0 Kudos
SamirGambhir
Frequent Contributor
Hi all,
I made some changes to the code and most of the issues are taken care of, except the infoWindow issue as described in my original post. Can someone help resolve this issue?
Here is the relevant code:
function init() {
...
        infoTemplate = new esri.InfoTemplate();
        stateFeatureLayer = new esri.layers.FeatureLayer("http://localhost:6080/arcgis/rest/services/State_profile/MapServer/1", {
  mode : esri.layers.FeatureLayer.MODE_ONDEMAND,
  outFields : ["*"],
  id : "stateFeatureLayer",
  infoTemplate : infoTemplate
 });
 distFeatureLayer = new esri.layers.FeatureLayer("http://localhost:6080/arcgis/rest/services/District_profile/MapServer/1", {
  mode : esri.layers.FeatureLayer.MODE_ONDEMAND,
  outFields : ["*"],
  id : "distFeatureLayer",
  infoTemplate : infoTemplate
 });
...
}

function queryParam(indc, indValue, flag, geog) { //indc=Indicator, indValue=indicator value, flag returns 'true' or 'false' depending on unit of analysis, geog is the selected State of India using a pull down menu
 var newTitle;
        query = new esri.tasks.Query();
 query.returnGeometry = true;
 query.outFields = ["*"];
 query.where = "1=1";
 var resultContent = "<tr>" + indc + ": <td>${" + indValue + ":formatNumber}</td></tr>";
 var renderer = new esri.renderer.ClassBreaksRenderer(symbol, indValue);
 renderer.addBreak(0.01, 19, symbolSet31);
 renderer.addBreak(19, 27, symbolSet32);
 renderer.addBreak(27, Infinity, symbolSet33);
 if (flag === true) {
  infoTemplate.setTitle("<tr>State: <td>${State_name}</tr></td>");
  stateFeatureLayer.setRenderer(renderer);
                if(distFeatureLayer){distFeatureLayer.hide();}
  stateFeatureLayer.show();
  newTitle = indc + " by State";
  dojo.connect(stateFeatureLayer, "onClick", showHighlight);
 } else if (flag === false) {
  infoTemplate.setTitle("<tr>DISTRICT: <td>${Dist_name}</tr></td><br /><tr>STATE: <td>${State_name}</tr></td>");
                if (geog != "All India") { //if a single State is selected. the code selects districts (sub-State) to display within the selected State
          distFeatureLayer.setDefinitionExpression("State_name= '" + geog + "'");
   distFeatureLayer.selectFeatures(query, esri.layers.FeatureLayer.SELECTION_NEW);
  } else if (geog == "All India") { //if "All India" is selected using the pull down menu. This displays all districts in the country
   distFeatureLayer.clearSelection();
                        distFeatureLayer.setDefinitionExpression(null);
  }
                distFeatureLayer.setRenderer(renderer);
                if(stateFeatureLayer){stateFeatureLayer.hide();}
  distFeatureLayer.show();
  newTitle = indc + " by District";
  dojo.connect(distFeatureLayer, "onClick", showHighlight);
 }
 infoTemplate.setContent(resultContent);
        showLegend(newTitle, flag);
}

function showLegend(title, flag){
        var legendDijit = dijit.byId("legend");
 dojo.connect(mapM, "onLayersAddResult", function(results) {
  var layerInfo = dojo.map(results, function(layer, index) {
   if (flag == true) {
      return {layer : stateFeatureLayer, title :title};
   } else if (flag == false) {
      return {layer : distFeatureLayer, title : title};
   }
  });
  if (layerInfo.length > 0) {
   if (legendDijit) {
    legendDijit.destroyRecursive(true);
   }
   legendDijit = new esri.dijit.Legend({
    map : mapM,
    layerInfos : layerInfo
   }, "legend");
   legendDijit.startup();
   legendDijit.refresh();
  }
 });
 if (flag == true) {
  mapM.addLayers([stateFeatureLayer]);
 } else if (flag == false) {
  mapM.addLayers([distFeatureLayer]);
 }
}

function showHighlight(evt){
        mapM.graphics.clear();
 var content = evt.graphic.getContent();
 mapM.infoWindow.setContent(content);
 var title = evt.graphic.getTitle();
 mapM.infoWindow.setTitle(title);
 mapM.infoWindow.resize(280, 75);
 var highlightGraphic = new esri.Graphic(evt.graphic.geometry, highlightSymbol);
 mapM.graphics.add(highlightGraphic);
 mapM.infoWindow.show(evt.screenPoint, mapM.getInfoWindowAnchor(evt.screenPoint));
 dojo.connect(mapM.infoWindow, "onHide", clearGraphics);
}


Hi, 
I resolved the second issue by setting the definition expression to 'null' when "geog=='All India'": distFeatureLayer.setDefinitionExpression(null); 
However, a new issue has cropped up: infoWindow works at the very first selection. Correct legend shows up as well at the first selection. Any subsequent selection does not bring up both. Also, for any subsequent selection of a different indicator value, anuncaught error shows up:"  Uncaught TypeError: Cannot read property 'firstChild' of null". I have checked my code and I do not find any reference to firstChild in my code. Additionally, switching between State and District brings up another uncaught error: "  Uncaught TypeError: Cannot call method '_decRefCount' of null". I am unable to make sense of all this. Has anybody faced a similar situation? I'll be happy to share my code if someone might want to look at this issue. 
Thanks 
Samir
0 Kudos