Select to view content in your preferred language

Seeking Geocoder Sample with a zoom in to an IdentifyTask

629
1
10-03-2013 12:49 PM
AveryGomez
Emerging Contributor
Does anyone have a sample you can forward to me where the geocoder zooms to the address and pops up an identifying task?

I know I saw something like this once and am not having any luck locating it again. Please let me know..

Thanks..
0 Kudos
1 Reply
TracySchloss
Honored Contributor
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);
            }       
      } 
0 Kudos