8.3 Census Map: Querying ArcGIS Server

Now that you have the onclick event listener for the map, let’s set it up so that it calls ArcGIS Server, and performs a spatial query to find data for the latitude/longitude location based on the user click.

For this tutorial, we will use a published map service that contains a census tract layer for the entire country. This layer has some basic 2000 census demographic data in its attributes:

https://whippet.ats.ucla.edu/ArcGIS/rest/services/up206b/week9/MapServer/0/

We learned that we can query this layer by supplying latitude and longitude values as follows:

https://whippet.ats.ucla.edu/ArcGIS/rest/services/up206b/week9/MapServer/0/query?geometry='+lng+','+lat+'&geometryType=esriGeometryPoint&spatialRel=esriSpatialRelIntersects&returnGeometry=true&outFields=*&f=pjson&callback=?

Use .getJSON to get data from ArcGIS Server

Let’s now use jQuery’s .getJSON function to get data from the ArcGIS Server url string in the getAGSData function, and trace our results in the debug console (lines 19-25):

 

 

Now every time a user clicks on the map, it makes a dynamic “point-in-polygon” call to ArcGIS Server, and gets data back based on what census tract the clicked latitude and longitude falls in.

Draw the census tract polygon clicked

ArcGIS Server also returns the geometry for the clicked census tract in GeoRSS format. The following function ,written by “Geo Jason” and modified slightly to read ArcGIS’s geometry output, allows us to draw polygons on Google Maps. Add this function to the code:

 

Now we are ready to draw a polygon based on the geometry returned from ArcGIS Server. First, declare a new variable for the highlighted polygon in the global space:

 

Next, add the call to draw the polygon within the getAGSdata function. First, erase any existing polygons (lines 9-13). Then, make the call to generate the polygon inside the .getJSON call (highlighted lines below).

 

 

Now when a user clicks on the map, it detects the latitude longitude coordinates, sends it to ArcGIS Server, gets the Geometry of the census tract polygon, and draws the polygon on the map.

census tract polygon drawn on map click

census tract polygon drawn on map click

(sample code)

Leave a Reply