getting census data given an address

567
2
07-12-2011 05:29 PM
NelsonEpifanio
New Contributor
After using locator, what do I need to do to get the census data (block and tract in particular) for the particular address? The info in available by creating a querytask to this url http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/0

I don't have any idea what information from the locator function I'm supposed to pass to get the info I need. 

Thanks,
0 Kudos
2 Replies
StephenLead
Regular Contributor III
After using locator, what do I need to do to get the census data (block and tract in particular) for the particular address? The info in available by creating a querytask to this url http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/0


see the sample at http://help.arcgis.com/en/webapi/javascript/arcgis/help/jssamples/query_nomap.html which shows how to query a dataset to return the values.

In this sample, they use
query.text = stateName;


In your case, you would use

query.geometry = pt


where pt is the point result of your address query. See the Query help file more more info.

Steve
0 Kudos
ChrisBuckmaster1
Occasional Contributor
This is something I am looking at as well but am having problems...

The idea is that the user enters an address, resulting candidate addresses are populated in a dojox data grid, the user then clicks on an address which then returns whether that address is within the query layer I have specified.

My code is below - I have tried to include setting the query geometry inside the row click handler function using the candidate.location details but it seems to throw an issue about the wkid spatial reference being undefined - is there anything glaringly wrong with this? I am trying to populate a div if the map point is within the query layer.

//CODE
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=7,IE=9" />
    <!--The viewport meta tag is used to improve the presentation and behavior of the samples
      on iOS devices-->
    <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"/>
    <title>RIMS Find</title>
    <link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/2.4/js/dojo/dijit/themes/claro/claro.css">
<link rel="stylesheet" type="text/css" href="http://rbcarcgis/html/js/dojo/1.6.1/dojox/grid/resources/Grid.css">
   <link rel="stylesheet" type="text/css" href="http://rbcarcgis/html/js/dojo/1.6.1/dojox/grid/resources/claroGrid.css">
    <style>
      html, body { height: 100%; width: 100%; margin: 0; padding: 0; }
      .esriScalebar{
        padding: 20px 20px;
      }
      #map{
        padding:0;
      }
    </style>
    <script type="text/javascript">var djConfig = {parseOnLoad: true};</script>
    <script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=2.4"></script>
<script type="text/javascript" src="https://getfirebug.com/firebug-lite.js"></script>
    <script type="text/javascript">
      dojo.require("dijit.layout.BorderContainer");
      dojo.require("dijit.layout.ContentPane");
      dojo.require("esri.map");
   dojo.require("dojox.grid.DataGrid");
      dojo.require("dojo.data.ItemFileReadStore");
   dojo.require("dijit.form.Button");
   dojo.require("dijit.form.TextBox");
   dojo.require("esri.tasks.locator");
   dojo.require("esri.tasks.query");

     
      var map;
      var resizeTimer;
   var locator, grid, store, geom, symbol, selectedSymbol, graphic, mapPnt;
   var queryTask, query;
     
      function init() {
  dojo.connect(grid, "onRowClick", onRowClickHandler);
 
        var initExtent = new esri.geometry.Extent({"xmin":492116,"ymin":160396,"xmax":511437,"ymax":174034,"spatialReference":{"wkid":27700}});
        map = new esri.Map("map",{extent:initExtent});
        //Add the topographic layer to the map. View the ArcGIS Online site for services http://arcgisonline/home/search.html?t=content&f=typekeywords:service   
        var basemap = new esri.layers.ArcGISTiledMapServiceLayer("http://rbcarcgis/ArcGIS/rest/services/runnymedestreet/MapServer");
        map.addLayer(basemap);
 
  locator = new esri.tasks.Locator("http://rbcarcgis/ArcGIS/rest/services/locator/GeocodeServer");
        dojo.connect(locator, "onAddressToLocationsComplete", showResults);
 
  //build query
        queryTask = new esri.tasks.QueryTask("http://rbcarcgis/ArcGIS/rest/services/rimsdata/MapServer/33");
 

        //build query filter
        query = new esri.tasks.Query();
        query.returnGeometry = false;
       
        //resize the map when the browser resizes - view the 'Resizing and repositioning the map' section in
        //the following help topic for more details http://help.esri.com/EN/webapi/javascript/arcgis/help/jshelp_start.htm#jshelp/inside_guidelines.htm
        dojo.connect(map, 'onLoad', function(theMap) {
          dojo.connect(dijit.byId('map'), 'resize', function() {  //resize the map if the div is resized
            clearTimeout(resizeTimer);
            console.log('resize');
            resizeTimer = setTimeout( function() {
              map.resize();
              map.reposition();
            }, 500);
          });
        });
      }
  
   function locate() {
        map.graphics.clear();
  var searchResult = dijit.byId("addressSearch").value;
        var address = {"Single Line Input": searchResult};
  locator.addressToLocations(address,["Ref_ID, Score, Match_addr"]);
      }

      function showResults(candidates) {
        symbol = new esri.symbol.PictureMarkerSymbol('http://rbcarcgis/html/graphics/HOUSE_ICON_VVSMALL.png', 25, 25);
  selectedSymbol = new esri.symbol.PictureMarkerSymbol('http://rbcarcgis/html/graphics/HOUSE_ICON_VVSMALL.png', 25, 25);

  //create array of attributes
        var items = dojo.map(candidates,function(candidate){
            geom = new esri.geometry.Point(candidate.location.x, candidate.location.y);
   var attributes = candidate.attributes;
            graphic = new esri.Graphic(geom, symbol, attributes);
            //add a graphic to the map at the geocoded location
            map.graphics.add(graphic);
   graphic.hide();
            return candidate.attributes; //break out of loop after one candidate with score greater  than 80 is found.
  });
 

       var data = {
          identifier: "Ref_ID",  //This field needs to have unique values
          label: "Ref_ID", //Name field for display. Not pertinent to a grid but may be used elsewhere.
          items: items
        };
  //Create data store and bind to grid.
        store = new dojo.data.ItemFileReadStore({ data:data });
        var grid = dijit.byId('grid');
        grid.setStore(store);
      }
  
   function onRowClickHandler(evt){
        var clickedAddressId = grid.getItem(evt.rowIndex).Ref_ID;
        var selectedAddress;

        dojo.forEach(map.graphics.graphics,function(graphic){
          if((graphic.attributes) && graphic.attributes.Ref_ID === clickedAddressId){
            selectedAddress = graphic;
   selectedAddress.show();
   selectedAddress.setSymbol(selectedSymbol);
            return;
          }else{
    graphic.hide();
   
    }
        });
  mapPnt = new esri.geometry.Point( {"x": selectedAddress.geometry.x, "y": selectedAddress.geometry.y," spatialReference": {" wkid": 27700 } });
  map.centerAndZoom(mapPnt,7);
  query.geometry = mapPnt;
  queryTask.execute(query, showQueryResults, error);
 
      }
  
  
   function showQueryResults(){
  
   var text = "This property IS within the Green Belt";
   dojo.byId("queryres").innerHTML = text;
  
   }
  
  
   function error(){
  
   alert("Error!");
  
   }
  

      dojo.addOnLoad(init);
    </script>
  </head>
 
  <body class="claro">
   <div dojoType="dijit.form.TextBox" style="width: 25em;" id="addressSearch" placeHolder="Address..."></div>
        <button dojotype="dijit.form.Button" onclick="locate()"> Locate</button>  
      <div dojotype="dijit.layout.ContentPane" id="resultspane" region="left" style="height: 200px; width: 300px;">
  <table dojotype="dojox.grid.DataGrid" data-dojo-id="grid" id="grid" data-dojo-props="rowsPerPage:'5', rowSelector:'20px'">
   <thead>
   <tr>
   <th field="Score">Match</th>
   <th field="Match_addr" width="70%">Address</th>
   </tr>
   </thead>
  </table>
   </div> 
      <div id="map" dojotype="dijit.layout.ContentPane" region="center" style="position: absolute; right: 2px; top: 2px; width: 40%; height: 40%; border:1px solid #000;padding:0;">
     </div>
  <div id="queryres"></div>
  </body>

</html>
//CODE
0 Kudos