Select to view content in your preferred language

Problems reactivating identify tool

849
3
06-03-2011 05:46 AM
ChrisBuckmaster1
Deactivated User
Hi

I am trying to implement a toolbar with identify and draw functions initially on it but am having some problems.

When I activate the identify tool for the first time it works fine, but if I were to change to the draw tool then back to the identify tool, my infowindow does not pop up, firebug gives me the following message:-

TypeError: Cannot call method 'setContent' of undefined{...}


There appears to be an issue specifically with how I am setting my content to show but am not too sure why it would initially work, then fail on the next call.

Can anyone see any problems with the below code? I am just including how the tool is activated and the identify function so if other bits are required I can post these up.

Many thanks!



//CODE TO SET THE DIFFERENT TOOLS

function turnidTool(ident)
    {
        idTool = ident;
        if (idTool==0)
        {
            if (drawHandle!=null)
            {
                dojo.disconnect(drawHandle); 
                drawHandle = null;             
            }
            if (identifyHandle!=null)
            {
                dojo.disconnect(identifyHandle); 
                resetFunctionality();
                identifyHandle = null;
            }  
        }
           
        if (idTool==1)
        {
            if (drawHandle!=null)
            {
                dojo.disconnect(drawHandle);
                drawHandle = null;             
            }
            if (identifyHandle==null)
            {
    identifyHandle = dojo.connect(map, "onClick", doIdentify);
            }  
        }
 
  if (idTool==2)
        {
   if (identifyHandle!=null)
            {
                dojo.disconnect(identifyHandle); 
                resetFunctionality();
                identifyHandle = null;
            }
            if (drawHandle==null)
            {
                drawHandle = dojo.connect(map, "onClick", doDraw);          
            }
        }
 
}


//CODE FOR THE IDENTIFY TOOL

  function resetFunctionality()
    {
        identifyTask = null;
        identifyParams = null;
        symbol = null;
    }

      function initFunctionality(map) {
   if (drawHandle!=null)
        {
            dojo.disconnect(drawHandle);  
            drawHandle = null;          
        }

        identifyTask = new esri.tasks.IdentifyTask("http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/BloomfieldHillsMichigan/Parcels/MapServer");
        identifyParams = new esri.tasks.IdentifyParameters();
        identifyParams.tolerance = 3;
        identifyParams.returnGeometry = true;
        identifyParams.layerIds = [0,2];
        identifyParams.layerOption = esri.tasks.IdentifyParameters.LAYER_OPTION_ALL;
        identifyParams.width  = map.width;
        identifyParams.height = map.height;
       
        map.infoWindow.resize(400, 250);
        map.infoWindow.setContent(dijit.byId("tabs").domNode);
        map.infoWindow.setTitle("Identify Results");

        symbol = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([255,0,0]), 2), new dojo.Color([255,255,0,0.25]));
      }

      function doIdentify(evt) {
   if (identifyParams==null)
        {
            initFunctionality(map);
        }
        map.graphics.clear();
        identifyParams.geometry = evt.mapPoint;
        identifyParams.mapExtent = map.extent;
        identifyTask.execute(identifyParams, function(idResults) { addToMap(idResults, evt); });
      }

      function addToMap(idResults, evt) {
        bldgResults = {displayFieldName:null,features:[]};
        parcelResults = {displayFieldName:null,features:[]};

        for (var i=0, il=idResults.length; i<il; i++) {
          var idResult = idResults;
          if (idResult.layerId === 0) {
            if (!bldgResults.displayFieldName) {bldgResults.displayFieldName = idResult.displayFieldName};
            bldgResults.features.push(idResult.feature);
          }
          else if (idResult.layerId === 2) {
            if (!parcelResults.displayFieldName) {parcelResults.displayFieldName = idResult.displayFieldName};
            parcelResults.features.push(idResult.feature);
          }
        }
        dijit.byId("bldgTab").setContent(layerTabContent(bldgResults,"bldgResults"));
  dijit.byId("parcelTab").setContent(layerTabContent(parcelResults,"parcelResults"));

        map.infoWindow.show(evt.screenPoint, map.getInfoWindowAnchor(evt.screenPoint));
      }

      function layerTabContent(layerResults, layerName) {
        var content = "";
        switch (layerName) {
          case "bldgResults":
    if (bldgResults.features.length !== 0){
   content = "<b>Building Footprints</b></br>";
            content += "<i>Total features returned: " + layerResults.features.length + "</i>";
            content += "<table border='1'><tr><th>ID</th><th>Address</th></tr>";
            for (var i=0, il=layerResults.features.length; i<il; i++) {
              content+="<tr><td>"+layerResults.features.attributes['PARCELID']+" <a href='#' onclick='showFeature(" + layerName + ".features[" + i + "]); return false;'>(show)</a></td>";
              content+="<td>"+layerResults.features.attributes['Full Site Address']+"</td>";
            }
            content+="</tr></table>";
   }
            break;
          case "parcelResults":
    if (parcelResults.features.length !== 0){
     content = "<b>Tax Parcels</b></br>";
            content += "<i>Total features returned: " + layerResults.features.length + "</i>";
            content += "<table border='1'><tr><th>ID</th><th>Year Built</th><th>School District</th><th>Description</th></tr>";
           for (var i=0, il=layerResults.features.length; i<il; i++) {
              content+="<tr><td>"+layerResults.features.attributes['Parcel Identification Number']+" <a href='#' onclick='showFeature(" + layerName + ".features[" + i + "]); return false;'>(show)</a></td>";
              content+="<td>"+layerResults.features.attributes['Residential Year Built']+"</td>";
              content+="<td>"+layerResults.features.attributes['School District Description']+"</td>";
              content+="<td>"+layerResults.features.attributes['Property Description']+"</td>";
            }
            content+="</tr></table>";
   }
            break;
        }
        return content;
      }
0 Kudos
3 Replies
HemingZhu
Frequent Contributor
Hi

I am trying to implement a toolbar with identify and draw functions initially on it but am having some problems.

When I activate the identify tool for the first time it works fine, but if I were to change to the draw tool then back to the identify tool, my infowindow does not pop up, firebug gives me the following message:-

TypeError: Cannot call method 'setContent' of undefined{...}


There appears to be an issue specifically with how I am setting my content to show but am not too sure why it would initially work, then fail on the next call.

Can anyone see any problems with the below code? I am just including how the tool is activated and the identify function so if other bits are required I can post these up.

Many thanks!



//CODE TO SET THE DIFFERENT TOOLS

function turnidTool(ident)
    {
        idTool = ident;
        if (idTool==0)
        {
            if (drawHandle!=null)
            {
                dojo.disconnect(drawHandle); 
                drawHandle = null;             
            }
            if (identifyHandle!=null)
            {
                dojo.disconnect(identifyHandle); 
                resetFunctionality();
                identifyHandle = null;
            }  
        }
           
        if (idTool==1)
        {
            if (drawHandle!=null)
            {
                dojo.disconnect(drawHandle);
                drawHandle = null;             
            }
            if (identifyHandle==null)
            {
    identifyHandle = dojo.connect(map, "onClick", doIdentify);
            }  
        }
 
  if (idTool==2)
        {
   if (identifyHandle!=null)
            {
                dojo.disconnect(identifyHandle); 
                resetFunctionality();
                identifyHandle = null;
            }
            if (drawHandle==null)
            {
                drawHandle = dojo.connect(map, "onClick", doDraw);          
            }
        }
 
}


//CODE FOR THE IDENTIFY TOOL

  function resetFunctionality()
    {
        identifyTask = null;
        identifyParams = null;
        symbol = null;
    }

      function initFunctionality(map) {
   if (drawHandle!=null)
        {
            dojo.disconnect(drawHandle);  
            drawHandle = null;          
        }

        identifyTask = new esri.tasks.IdentifyTask("http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/BloomfieldHillsMichigan/Parcels/MapServer");
        identifyParams = new esri.tasks.IdentifyParameters();
        identifyParams.tolerance = 3;
        identifyParams.returnGeometry = true;
        identifyParams.layerIds = [0,2];
        identifyParams.layerOption = esri.tasks.IdentifyParameters.LAYER_OPTION_ALL;
        identifyParams.width  = map.width;
        identifyParams.height = map.height;
       
        map.infoWindow.resize(400, 250);
        map.infoWindow.setContent(dijit.byId("tabs").domNode);
        map.infoWindow.setTitle("Identify Results");

        symbol = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([255,0,0]), 2), new dojo.Color([255,255,0,0.25]));
      }

      function doIdentify(evt) {
   if (identifyParams==null)
        {
            initFunctionality(map);
        }
        map.graphics.clear();
        identifyParams.geometry = evt.mapPoint;
        identifyParams.mapExtent = map.extent;
        identifyTask.execute(identifyParams, function(idResults) { addToMap(idResults, evt); });
      }

      function addToMap(idResults, evt) {
        bldgResults = {displayFieldName:null,features:[]};
        parcelResults = {displayFieldName:null,features:[]};

        for (var i=0, il=idResults.length; i<il; i++) {
          var idResult = idResults;
          if (idResult.layerId === 0) {
            if (!bldgResults.displayFieldName) {bldgResults.displayFieldName = idResult.displayFieldName};
            bldgResults.features.push(idResult.feature);
          }
          else if (idResult.layerId === 2) {
            if (!parcelResults.displayFieldName) {parcelResults.displayFieldName = idResult.displayFieldName};
            parcelResults.features.push(idResult.feature);
          }
        }
        dijit.byId("bldgTab").setContent(layerTabContent(bldgResults,"bldgResults"));
  dijit.byId("parcelTab").setContent(layerTabContent(parcelResults,"parcelResults"));

        map.infoWindow.show(evt.screenPoint, map.getInfoWindowAnchor(evt.screenPoint));
      }

      function layerTabContent(layerResults, layerName) {
        var content = "";
        switch (layerName) {
          case "bldgResults":
    if (bldgResults.features.length !== 0){
   content = "<b>Building Footprints</b></br>";
            content += "<i>Total features returned: " + layerResults.features.length + "</i>";
            content += "<table border='1'><tr><th>ID</th><th>Address</th></tr>";
            for (var i=0, il=layerResults.features.length; i<il; i++) {
              content+="<tr><td>"+layerResults.features.attributes['PARCELID']+" <a href='#' onclick='showFeature(" + layerName + ".features[" + i + "]); return false;'>(show)</a></td>";
              content+="<td>"+layerResults.features.attributes['Full Site Address']+"</td>";
            }
            content+="</tr></table>";
   }
            break;
          case "parcelResults":
    if (parcelResults.features.length !== 0){
     content = "<b>Tax Parcels</b></br>";
            content += "<i>Total features returned: " + layerResults.features.length + "</i>";
            content += "<table border='1'><tr><th>ID</th><th>Year Built</th><th>School District</th><th>Description</th></tr>";
           for (var i=0, il=layerResults.features.length; i<il; i++) {
              content+="<tr><td>"+layerResults.features.attributes['Parcel Identification Number']+" <a href='#' onclick='showFeature(" + layerName + ".features[" + i + "]); return false;'>(show)</a></td>";
              content+="<td>"+layerResults.features.attributes['Residential Year Built']+"</td>";
              content+="<td>"+layerResults.features.attributes['School District Description']+"</td>";
              content+="<td>"+layerResults.features.attributes['Property Description']+"</td>";
            }
            content+="</tr></table>";
   }
            break;
        }
        return content;
      }


Seems like you have two event handlers dealing with onclick event. It would be much easier to have one handler to dealing with two click scenarios, that way you don't have to connect one event and disconnect another. All the actions can be easy handled in one handler. Something like:

var actionMode =0;  //globle variable, default as do nothing
....
dojo.connect(map, "onClick", clickAction);
....
function clickAction (evt)
{
     switch (actionMode)
     {
         case 0:
           return;          
           //break;
         case 1:
           doIdentify();
           break;
         case 2:
           doDraw();
           break;
     }  
}
....
function turnidTool(ident)
{
     actionMode =ident;
}

Hope this code snippets make sense to you.
0 Kudos
ChrisBuckmaster1
Deactivated User
Many thanks for your reply hzhu.

I don't think the issue is how the handlers are initialised, it appears to be an issue with setting the content using the 'domnode'.

I found this related topic which appears to illustrate the same problem I am having where the divs are not being loaded on a second request...

http://forums.esri.com/Thread.asp?c=158&f=2396&t=295163

Unfortunately there does not seem to be a solution in there 😞
0 Kudos
RyanStrain
Emerging Contributor
Was there any resolution, or work around method discovered for this issue? Is there a way to set the content without using the domnode? I'm encountering the same issue when revisiting a service and using the attribute inspector infowindow. Thanks.
0 Kudos