Use the address from a Search as the point to identify a county

2970
3
Jump to solution
08-20-2015 09:02 AM
TracySchloss
Frequent Contributor

I want to be able to use the point of an address as the input geometry to my IdentifyTask.  I'm having a scope problem.  I can see that my identify task is finding the county, but I'm not calling/returning that name properly.  I'm sure I'm not using lang.hitch properly, but I can tell that select-result was completing before runIdentify ever started.

searchTool is my Search widget.

       on(searchTool,'select-result', function (evt){
         var addr = evt.result.name;
         app.addrExt = evt.result.extent;
         myIdentify.runIdentify().then(lang.hitch(this,function(response){
             runQueries(app.countyName);
           })
         )
       })

myIdentify.js

define ([ "dojo/on", "esri/tasks/IdentifyTask","esri/tasks/IdentifyParameters",
"esri/geometry/Point","esri/geometry/Extent","esri/geometry/webMercatorUtils"
      ], function (  on,IdentifyTask,IdentifyParameters,Point,Extent,webMercatorUtils
) {  
    return {
      initIdentify: function(url){
        app.idTask = new IdentifyTask(url);
        app.idParams = new IdentifyParameters();
      },
      runIdentify: function(){         
          var webPt = webMercatorUtils.lngLatToXY(app.addrExt.xmin, app.addrExt.ymin);
          var point = new Point(webPt[0], webPt[1], app.spatialReference);
          app.idParams.geometry = point;
          var pxWidth = app.map.extent.getWidth() / app.map.width;
          var padding = 3 * pxWidth;
          var qGeom = new Extent({
              "xmin": point.x - padding,
              "ymin": point.y - padding,
              "xmax": point.x + padding,
              "ymax": point.y + padding,
              "spatialReference": app.spatialReference
          });
          app.idParams.geometry = point;
          app.idParams.mapExtent = qGeom;
          app.idParams.tolerance = 1;
          app.idParams.returnGeometry = true;
          app.idTask.execute(app.idParams).then(function(results){
            app.countyName = results[0].value;  // the identify is finding the county name here, I'm just not returning it properly
          });
          return app.countyName;      
      }
    }
});  
0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Tracy,

  The idTask.execute is a promise so that is fine but your run identify need to be a Deferred.

Something like this (untested)

      runIdentify: function(){ 
          var def = new Deferred();          
          var webPt = webMercatorUtils.lngLatToXY(app.addrExt.xmin, app.addrExt.ymin);  
          var point = new Point(webPt[0], webPt[1], app.spatialReference);  
          app.idParams.geometry = point;  
          var pxWidth = app.map.extent.getWidth() / app.map.width;  
          var padding = 3 * pxWidth;  
          var qGeom = new Extent({  
              "xmin": point.x - padding,  
              "ymin": point.y - padding,  
              "xmax": point.x + padding,  
              "ymax": point.y + padding,  
              "spatialReference": app.spatialReference  
          });  
          app.idParams.geometry = point;  
          app.idParams.mapExtent = qGeom;  
          app.idParams.tolerance = 1;  
          app.idParams.returnGeometry = true;  
          app.idTask.execute(app.idParams).then(lang.hitch(this, function(results){  
            app.countyName = results[0].value;  // the identify is finding the county name here, I'm just not returning it properly  
            def.resolve(app.countyName);
          }));  
          return def;        
      }

View solution in original post

0 Kudos
3 Replies
RobertScheitlin__GISP
MVP Emeritus

Tracy,

  The idTask.execute is a promise so that is fine but your run identify need to be a Deferred.

Something like this (untested)

      runIdentify: function(){ 
          var def = new Deferred();          
          var webPt = webMercatorUtils.lngLatToXY(app.addrExt.xmin, app.addrExt.ymin);  
          var point = new Point(webPt[0], webPt[1], app.spatialReference);  
          app.idParams.geometry = point;  
          var pxWidth = app.map.extent.getWidth() / app.map.width;  
          var padding = 3 * pxWidth;  
          var qGeom = new Extent({  
              "xmin": point.x - padding,  
              "ymin": point.y - padding,  
              "xmax": point.x + padding,  
              "ymax": point.y + padding,  
              "spatialReference": app.spatialReference  
          });  
          app.idParams.geometry = point;  
          app.idParams.mapExtent = qGeom;  
          app.idParams.tolerance = 1;  
          app.idParams.returnGeometry = true;  
          app.idTask.execute(app.idParams).then(lang.hitch(this, function(results){  
            app.countyName = results[0].value;  // the identify is finding the county name here, I'm just not returning it properly  
            def.resolve(app.countyName);
          }));  
          return def;        
      }
0 Kudos
TracySchloss
Frequent Contributor

I don't think I'm calling it correctly either.   Fixing myIdentify.js, I'm still getting Cannot read properly 'then' of undefined on the line

var name = myIdentify.runIdentify().then(lang.hitch(this,function(response){

0 Kudos
TracySchloss
Frequent Contributor

I don't think I changed anything, but suddenly it decided to work!  I had several break points set.  Once I took them out, it was fine.  Usually it's the break points that pause the script long enough to let it finish executing.

0 Kudos