Description
This example queries the map for census blocks within the current map extent. The user can then click on a census block to see an info window with a chart of the demographic makeup of the census block. The chart is created with the Google Chart API.
這是個有關查詢當前地圖範圍的街區人口普查資料的例子。我們可以通過在查詢所得到的圖層上點擊相應的街區來查看利用這個街區的人口資料產生的餅圖。這個餅圖是通過 Google Chart API產生的。
To accomplish the query, the example uses QueryTask, a class specific to the ArcGIS JavaScript extension. The URL of the map layer to be queried is passed to the QueryTask constructor:
qtask = new esri.arcgis.gmaps.QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/1");
為了完成這個查詢,該例子使用了QueryTask對象,這個對象是通過 ArcGIS JavaScript extension定義的。產生一個QueryTask對象需要使用一個指向地圖地址的url,將這個url作為參數傳入construter,就可以了。
Notice the 1 at the end of the URL. This means that the layer with index number 1 in the ESRI_Census_USA map service will be queried. To find out the layer index numbers and URLs for a given map service, use the Services Directory.
在url中最後有一個index number,不同的index number有不同的作用,
The QueryTask requires a Query. The code specifies the bounding box of the area to be queried (query.queryGeometry) and the fields to return (query.outFields). The query is also told to return the geometry information of the features (query.returnGeometry).
在構造器中產生的QueryTask只是一個可供查詢的圖層,要真正地執行查詢,還需要一個Query。Query具有以下幾個參數:
所查詢地區的boundingbox(query.queryGeometry)
查詢結果的返回欄位(query.outFields)
是否返回查詢結果的圖形(query.returnGeometry = true;)
This line runs the task, thereby executing the query:
qtask.execute(query, false, mycallback);
The three arguments represent 1) the query conditions, 2) whether to return KML, and 3) a callback function that runs immediately after the query is executed.
執行查詢的語句:qtask.execute(query, false, mycallback);
這三個參數分別是指:1)查詢條件 2)是否返回kml 3)一個在查詢執行後馬上執行的callback函數
The callback function mycallback does the work of setting up the chart in the info window. The function takes as input the features returned from the query (fset). It loops through each feature and does the following things:
- Parses the information from the age fields
- Calculates the total population for the block group
- Calculates a percentage of the population for each age group
- Creates the text and images for the info window that will appear when someone clicks the feature. In doing so, the function passes the information on age distribution to the Google Chart API to create a chart. Look closely in the code for http://chart.apis.google.com/chart? and you can see where the Google Chart API is being referenced. See the Google Chart API Developer's Guide to learn about constructing charts in the Google Chart API.
- Adds the feature and its associated info window information to the map.
mycallback函數執行在info視窗中建立餅圖的功能。這個函數所需的參數是Query所得到的結果(fset數組),然後使用一個迴圈對數組中的每個元素做下面的事情 1.從年齡欄位裡面讀取資料,並將其轉為數字 2. 計算所有的人口 3.計算每個組中人口的比例 4.建立在資訊視窗中顯示的文字和餅圖(餅圖是通過將各個比例尺資料作為參數傳給Google Chart API得到) 5. 將features和相應的資訊視窗加入地圖樣本原始碼: