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; } } });
Solved! Go to Solution.
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; }
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; }
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){
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.