kl8828

geocode and identify

Discussion created by kl8828 on Dec 6, 2012
Latest reply on Dec 21, 2012 by schlot
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[i];
        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[i].name=="CAPCOG_City_Limits"){
                    cityContent = "<td>"+layerResults.features[i].attributes['NAME']+"</td>";
                }
                if(layerResults.features[i].name=="Central_CountyBoundaries"){
                    countyContent = "<td>"+layerResults.features[i].attributes['NAME']+"</td>";
                }
                if(layerResults.features[i].name=="School_ISD"){
                    schoolContent = "<td>"+layerResults.features[i].attributes['NAME']+"</td>";
                }
                if(layerResults.features[i].name=="TravisHealthDistrict"){
                    healthContent = "<td>"+layerResults.features[i].attributes['NAME']+"</td>";
                }
                if(layerResults.features[i].name=="PLAN_House_309"){
                    houseContent = "<td>"+layerResults.features[i].attributes['NAME']+"</td>";
                }
                if(layerResults.features[i].name=="PLAN_Senate_172"){
                    senateContent = "<td>"+layerResults.features[i].attributes['NAME']+"</td>";
                }
                if(layerResults.features[i].name=="Precincts"){
                    precinctContent = "<td>"+layerResults.features[i].attributes['NAME']+"</td>";
                }
                if(layerResults.features[i].name=="LCRA"){
                    lcraContent = "<td>"+layerResults.features[i].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);

Outcomes