I needed to have an identify function that served double duty, running from either a map click or a geocoded point.I have a Boolean variable set 'idClick'. I needed this because a click event you need event.mapPoint to determine the geometry for the identifyParameters and geocoding returns geometry already. Then these lines in my initiating function
identifyTask = new esri.tasks.IdentifyTask(http://yourserver/ArcGIS/rest/services/serviceName/MapServer");
identifyParams = new esri.tasks.IdentifyParameters();
identifyParams.tolerance = 1; //
identifyParams.returnGeometry = false;
identifyParams.layerIds = [0,1,2];
identifyParams.layerOption = esri.tasks.IdentifyParameters.LAYER_OPTION_VISIBLE;
identifyParams.spatialReference = spatialReference;
identifyParams.extent = map.extent;
identifyParams.width = map.width;
identifyParams.height = map.height;
dojo.connect(map, "onClick", function () {idFromClick = true;});
dojo.connect(map, "onClick", executeIdentifyTask);
Here is the execute function
//identify either from map click or geocoded location
function executeIdentifyTask(evt) {
identifyParams.mapExtent = map.extent.expand(0.1);
identifyParams.geometry = evt; //input from address, evt is already a map point
var inputPt = evt;
if (idFromClick) { // input from map click
identifyParams.geometry = evt.mapPoint;
inputPt = evt.mapPoint;
}
var deferred = identifyTask.execute(identifyParams);
deferred.addCallback(function(response) {
return dojo.map(response, function(result) {
var feature = result.feature;
var layerName = result.layerName;
feature.attributes.layerName = result.layerName;
feature.setInfoTemplate(plainTemplate);
return feature;
});
});
map.infoWindow.setFeatures([ deferred ]);
map.infoWindow.show(inputPt); //point type is the same coming from either method
}
Last you need to call this function at the end of your geocoding results function
function showResults(candidates) {
var candidate;
var symbol = new esri.symbol.SimpleMarkerSymbol();
var addressTemplate = new esri.InfoTemplate("Location", "Address: ${address}<br />");
symbol.setStyle(esri.symbol.SimpleMarkerSymbol.STYLE_SQUARE);
symbol.setColor(new dojo.Color([255,0,0,0.75]));
dojo.every(candidates,function(candidate){
console.log(candidate.score);
if (candidate.score > 80) {
console.log(candidate.location);
var attributes = { address: candidate.address, score:candidate.score, pt:candidate.location };
geometryPt = candidate.location;
var graphic = new esri.Graphic(geometryPt, symbol, attributes, addressTemplate);
map.graphics.add(graphic);
var displayText = candidate.address;
map.infoWindow.show(geometryPt);
map.infoWindow.setTitle("Address");
return false; //break out of loop after one candidate with score greater than 80 is found.
}
});
map.centerAndZoom(geometryPt,12);
executeIdentifyTask(geometryPt);
}
}