Description
This example shows how to return query results as KML. KML is an XML-based format for representing features and attributes. It's commonly used in Google Maps and other geographic applications. Returning query results in KML preserves the symbology originally set by the map author.
To run this example, pan to an area of the map click Execute Query. All of the highway features in the map extent will be overlaid on the map as a GeoXML overlay (KML). You can click a road segment to display an info window with attributes.
Click Clear Map Overlays to remove the KML features. Optionally, you can navigate to a new area of the map and execute the query again.
The initialize function sets up the map, adding all of the necessary controls and specifying the coordinates and zoom level that the map should use when the page opens. This function also creates a new MapExtension that will help display the results.
To prepare for the query, the function creates a new 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/Speciality/ESRI_StateCityHighway_USA/MapServer/0");The URL in the constructor references the highway layer that will be queried. In this case, it's the layer with index number 0 in the ESRI_StateCityHighway_USA map service. To find the URLs for the layers you want to query in your own maps, use the Services Directory.
The executeQuery function runs when someone clicks the Execute Query button. This function gets the bounding box of the map, removes any existing overlays, and sets up the query conditions. It tells the query to return information from the LENGTH, TYPE, ADMN_CLASS, TOLL_RD fields. Finally, the function uses the following line to run the task, thereby executing the query:
qtask.execute(query, true, mycallback);
The three arguments represent 1) the query conditions, 2) whether to return a GGeoXML overlay (KML), and 3) a callback function that runs immediately after the query is executed.
- The callback function mycallback adds the query results to the map, using the helper MapExtension class that was created in the initialize function.
- 從代碼上看,使用kml的方式和使用其它方式的不同之處僅在於使用kml時需將qtask.execute()中的是否返回kml項設為true,然後在mycallback中直接將kml類型的參數添加入地圖即可。
- 在效果上看,kml這種方式的查詢結果視覺效果更好,由於kml是基於xml的,我猜kml的載入速度也許也會快一些,但我沒有驗證,只是猜測而已。