geocode and identify

476
1
12-06-2012 08:02 PM
KyungahLim
New Contributor
I wish to use the geocoding task to identify all the layers at the point of the address.
Here I created a variable called geoResults to pass the x and y coordinates to the identify task.
The error I'm having is saying that it's not liking the following line, specifically mapPoint, which used to be (I think) screenPoint:
    map.infoWindow.show(evt.mapPoint, map.getInfoWindowAnchor(evt.mapPoint));

My goal is that as user inputs an address and pushes the button calling the locate() function, instead of a pop-up window showing the address and the locator information or whatnot, a pop-up window shows all the layers underneath the point.
I appreciate any ideas.





//////// Identify Task ///////////////

function initFunctionality(map) {
    dojo.connect(map, doIdentify);
    /*
     dojo.connect(map, "onClick", doIdentify);
     */


    identifyTask = new esri.tasks.IdentifyTask("http://54.245.107.147/ArcGIS/rest/services/everything/MapServer");

    identifyParams = new esri.tasks.IdentifyParameters();
    identifyParams.tolerance = 3;
    //identifyParams.spatialReference = map.spatialReference;
    identifyParams.returnGeometry = true;
    identifyParams.layerIds = [0,1,2,3,4,5,6,7];
    identifyParams.layerOption = esri.tasks.IdentifyParameters.LAYER_OPTION_ALL;
    identifyParams.width  = map.width;
    identifyParams.height = map.height;

    map.infoWindow.resize(715, 200);
    map.infoWindow.setContent(dijit.byId("tabs").domNode);
    map.infoWindow.setTitle("IdentifyResults");

    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() {
    //alert(geoResult);
    //map.graphics.clear();
    identifyParams.geometry = geoResult; //evt.mapPoint;
    identifyParams.mapExtent = map.extent;
    identifyTask.execute(identifyParams, function(idResults) { addToMap(idResults); });
}

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

    for (var i=0, il=idResults.length; i<il; i++) {
        var idResult = idResults;
        if (idResult.layerId === 0) {
            if (!identifyResults.displayFieldName) {identifyResults.displayFieldName = idResult.displayFieldName};
            identifyResults.features.push(idResult.feature);
        }
        else if (idResult.layerId === 1) {
            if (!identifyResults.displayFieldName) {identifyResults.displayFieldName = idResult.displayFieldName};
            identifyResults.features.push(idResult.feature);
        }
        else if (idResult.layerId === 2) {
            if (!identifyResults.displayFieldName) {identifyResults.displayFieldName = idResult.displayFieldName};
            identifyResults.features.push(idResult.feature);
        }
        else if (idResult.layerId === 3) {
            if (!identifyResults.displayFieldName) {identifyResults.displayFieldName = idResult.displayFieldName};
            identifyResults.features.push(idResult.feature);
        }
        else if (idResult.layerId === 4) {
            if (!identifyResults.displayFieldName) {identifyResults.displayFieldName = idResult.displayFieldName};
            identifyResults.features.push(idResult.feature);
        }
        else if (idResult.layerId === 5) {
            if (!identifyResults.displayFieldName) {identifyResults.displayFieldName = idResult.displayFieldName};
            identifyResults.features.push(idResult.feature);
        }
        else if (idResult.layerId === 6) {
            if (!identifyResults.displayFieldName) {identifyResults.displayFieldName = idResult.displayFieldName};
            identifyResults.features.push(idResult.feature);
        }
        else if (idResult.layerId === 7) {
            if (!identifyResults.displayFieldName) {identifyResults.displayFieldName = idResult.displayFieldName};
            identifyResults.features.push(idResult.feature);
        }
    }


    dijit.byId("identifyTab").setContent(layerTabContent(identifyResults,"identifyResults"));

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

function layerTabContent(layerResults, layerName) {
    var content = "";
    var cityContent = "";
    var countyContent = "";
    var schoolContent = "";
    var healthContent = "";
    var houseContent = "";
    var senateContent = "";
    var precinctContent = "";
    var lcraContent = "";

    switch (layerName) {
        case "identifyResults":
            content = "<i>Total features returned: " + layerResults.features.length + "</i>";
            content += "<table border='1'><tr><th>City<th>County</th><th>School</th><th>Health</th><th>House</th><th>Senate</th><th>Precincts</th><th>LCRA</th></tr><tr>";
            for (var i=0, il=layerResults.features.length; i<il; i++) {
                if(layerResults.features.name=="CAPCOG_City_Limits"){
                    cityContent = "<td>"+layerResults.features.attributes['NAME']+"</td>";
                }
                if(layerResults.features.name=="Central_CountyBoundaries"){
                    countyContent = "<td>"+layerResults.features.attributes['NAME']+"</td>";
                }
                if(layerResults.features.name=="School_ISD"){
                    schoolContent = "<td>"+layerResults.features.attributes['NAME']+"</td>";
                }
                if(layerResults.features.name=="TravisHealthDistrict"){
                    healthContent = "<td>"+layerResults.features.attributes['NAME']+"</td>";
                }
                if(layerResults.features.name=="PLAN_House_309"){
                    houseContent = "<td>"+layerResults.features.attributes['NAME']+"</td>";
                }
                if(layerResults.features.name=="PLAN_Senate_172"){
                    senateContent = "<td>"+layerResults.features.attributes['NAME']+"</td>";
                }
                if(layerResults.features.name=="Precincts"){
                    precinctContent = "<td>"+layerResults.features.attributes['NAME']+"</td>";
                }
                if(layerResults.features.name=="LCRA"){
                    lcraContent = "<td>"+layerResults.features.attributes['NAME']+"</td>";
                }


            }
            content += cityContent + countyContent + schoolContent + healthContent + houseContent + senateContent + precinctContent + lcraContent
            content += "</tr></table>";
            break;
    }
    return content;
}


function showFeature(feature) {
    map.graphics.clear();
    feature.setSymbol(symbol);
    map.graphics.add(feature);
}

/////////////// geocode ///////////////
function locate() {
    map.graphics.clear();
    var address = {"SingleLine":dojo.byId("address").value};
    locator.outSpatialReference= map.spatialReference;
    var options = {
        address:address,
        outFields:["Loc_name"]
    }
    locator.addressToLocations(options);
}

function showResults(candidates) {
    var candidate;
    var symbolResult = new esri.symbol.SimpleMarkerSymbol();
    var infoTemplate = new esri.InfoTemplate("Location", "Address: ${address}");

    symbolResult.setStyle(esri.symbol.SimpleMarkerSymbol.STYLE_CIRCLE);
    symbolResult.setColor(new dojo.Color([153,0,51,0.75]));

    var geom;
    var wgs = new esri.SpatialReference({ "wkid": 4326 });
    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, locatorName:candidate.attributes.Loc_name };
            geom = candidate.location;
            geoResult = new esri.geometry.Point(geom.x, geom.y, wgs);
            var graphic = new esri.Graphic(geom, symbolResult, attributes, infoTemplate);
            //add a graphic to the map at the geocoded location
            map.graphics.add(graphic);
            //add a text symbol to the map listing the location of the matched address.
            var displayText = candidate.address;
            var font = new esri.symbol.Font("16pt",esri.symbol.Font.STYLE_NORMAL, esri.symbol.Font.VARIANT_NORMAL,esri.symbol.Font.WEIGHT_BOLD,"Helvetica");

            var textSymbol = new esri.symbol.TextSymbol(displayText,font,new dojo.Color("#666633"));
            textSymbol.setOffset(0,8);
            map.graphics.add(new esri.Graphic(geom, textSymbol));
            return false; //break out of loop after one candidate with score greater  than 80 is found.
        }
    });
    if(geom !== undefined){
        map.centerAndZoom(geom,11);
        doIdentify();
    }

}

function extentHistoryChangeHandler() {
    dijit.byId("zoomprev").disabled = navToolbar.isFirstExtent();
    dijit.byId("zoomnext").disabled = navToolbar.isLastExtent();
}

dojo.addOnLoad(init);
0 Kudos
1 Reply
TracySchloss
Frequent Contributor
Did you get this figured out yet?  I got stuck in the same place, trying to sort out points vs. geometry etc.  Not only did I want to enter an address,  I still wanted my identifytask to work too.


I created a couple of variables
var geometryPt;
var idFromClick = true;


Your identify parameters are not anything different.  For me I added an onClick event on the map.  This gave me a place to call an identifyTask function that would work both from a click and from the results function of the locate. 

dojo.connect(map, "onClick", function () {idFromClick = true;});
dojo.connect(map, "onClick", executeIdentifyTask); 


This is my executeIdentifyTask function.  My identify was likely to return from multiple layers, so you'll need the same deferred technique if you do too.  You need to pay attention to what sort of geometry you are using in your identifyparameters.  When I call it from a click event, I needed to convert it to a mapPoint because it's only coordinates/geometry.  When you get the results of a locator, it's already a point, so you need to keep track of whether a click or a geocode is the input.

 
 function executeIdentifyTask(evt) {   
      identifyParams.mapExtent = map.extent.expand(0.1); 
        identifyParams.geometry = evt; 
     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) {      
          // response is an array of identify result objects     
          // Let's return an array of features. 
          return dojo.map(response, function(result) { 
            var feature = result.feature; 
            feature.setInfoTemplate(myTemplate);
              return feature;             
          }); 
        }); 
 
        // InfoWindow expects an array of features from each deferred object that you pass. If the response from the task execution  
        // above is not an array of features, then you need to add a callback like the one above to post-process the response and return an 
        // array of features. 
        map.infoWindow.setFeatures([ deferred ]); 
        map.infoWindow.resize(175,100);

        map.infoWindow.show(inputPt); //point type is the same coming from either method
     //map.infoWindow.show(evt.mapPoint);   
      } 
     

Then make a few modifications to the standard locate and showResults functions.  You can edit out the lines I have for "containsCounty".  I'm mapping a whole state and I needed to handle somehow a user entering "Jackson County, MO" or some other county name instead of a city.
    
  function locate() {   You can see at the very end I'm using the geocoded point as the input to the identify.
            idFromClick = false;  //executeIdentifyTask handles the type of point being passed. 
            map.infoWindow.hide();
            map.graphics.clear(); 
            geometryPt = "";
            var address = {"SingleLine":dojo.byId("txtAddress").value}; 
            
            locator.outSpatialReference= map.spatialReference; 
            var options = { 
              address:address, 
              outFields:["Loc_name"] 
            };
            locator.addressToLocations(options); 
      } 

       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); 
                    //add a graphic to the map at the geocoded location 
                    map.graphics.add(graphic); 
                    //add a text symbol to the map listing the location of the matched address. 
                    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. 
                   }         
                }); 
            if(geometryPt !== undefined){ 
                var containsCountyPosition = (dojo.byId("txtAddress").value.toLowerCase().indexOf(" county"));           
                      if (containsCountyPosition > 0) {
                      map.centerAndZoom(geometryPt,10); 
                    } else {
                         map.centerAndZoom(geometryPt,12); 
                    } 
                
                executeIdentifyTask(geometryPt);
            }      
0 Kudos