在地图上画多边形Ajax查询 1. 前台标签及脚本: <input id="Button1" type="button" value="画多边形查询" οnclick="spatialExtentSearch();" /></div> <div id="queryTableContainer"></div> <mce:script type="text/javascript"><!-- var map; function spatialExtentSearch() { var envelope = "98,41,125,21"; map = $find('Map1'); var keyDownGetGeometry = function() { map.getGeometry(ESRI.ADF.Graphics.ShapeType.Ring, drawGeometry, null, 'red', '#0000FF', 'pointer', true); }; var keyUpCancelGeometry = function() { map.cancelGetGeometry(); }; map.setKeyAction(65, keyDownGetGeometry, keyUpCancelGeometry, null, false); //CallServer(envelope); } function ReceiveServerData(rValue) { document.getElementById("queryTableContainer").innerHTML = rValue; } var graphicCount = 0; function drawGeometry(inputGeometry) { var coords = inputGeometry.getRing(0).toString("<br>", ","); var attributes = { "graphicID": graphicCount, "featureCoordinates": coords }; var graphicFeature = $create(ESRI.ADF.Graphics.GraphicFeature, { "id": graphicCount, "geometry": inputGeometry, "attributes": attributes }); graphicCount++; var graphicFeatureGroupID = "polygonGraphics"; var graphicFeatureGroup = $find(graphicFeatureGroupID); if (!graphicFeatureGroup) { var defaultSymbol = new ESRI.ADF.Graphics.FillSymbol('white', 'blue', 2, 'default'); defaultSymbol.set_opacity(0.5); var highlightSymbol = new ESRI.ADF.Graphics.FillSymbol('yellow', 'green', 4, 'pointer'); highlightSymbol.set_opacity(0.5); var graphicFeatureGroup = $create(ESRI.ADF.Graphics.GraphicFeatureGroup, { "id": graphicFeatureGroupID, "symbol": defaultSymbol, "highlightSymbol": highlightSymbol }); map.addGraphic(graphicFeatureGroup); } graphicFeatureGroup.add(graphicFeature); map.set_extent(graphicFeature.getEnvelope()); var envelope = graphicFeature.getEnvelope(); var xmin, xmax, ymin, ymax; xmin = envelope.get_xmin(); ymin = envelope.get_ymin(); xmax = envelope.get_xmax(); ymax = envelope.get_ymax(); var envelope = xmin + "," + ymin + "," + xmax + "," + ymax; CallServer(envelope); } // --></mce:script> 2.服务器端脚本: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Text; namespace BoxSearchApp { public partial class _Default : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler { protected String returnValue; protected void Page_Load(object sender, EventArgs e) { String cbReference = Page.ClientScript.GetCallbackEventReference(this, "arg", "ReceiveServerData", "context"); String callbackScript; callbackScript = "function CallServer(arg, context)" + "{ " + cbReference + ";}"; Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "CallServer", callbackScript, true); } public void RaiseCallbackEvent(String eventArgument) { string[] envelope = eventArgument.ToString().Split(new Char[] { ',' }); double xmin = Convert.ToDouble(envelope[0]); double ymin = Convert.ToDouble(envelope[1]); double xmax = Convert.ToDouble(envelope[2]); double ymax = Convert.ToDouble(envelope[3]); StringBuilder sb = new StringBuilder(); sb.Append("<table border='1px'>"); foreach (ESRI.ArcGIS.ADF.Web.DataSources.IGISFunctionality gisFunctionality in Map1.GetFunctionalities()) { ESRI.ArcGIS.ADF.Web.DataSources.IGISResource gisResource = gisFunctionality.Resource; bool supported = gisResource.SupportsFunctionality(typeof(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality)); if (supported) { ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality queryFunctionality = (ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality)gisResource.CreateFunctionality (typeof(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality), null); string[] layerIDs = null; string[] layerNames = null; queryFunctionality.GetQueryableLayers(null, out layerIDs, out layerNames); ESRI.ArcGIS.ADF.Web.Geometry.Envelope adfEnvelope = new ESRI.ArcGIS.ADF.Web.Geometry.Envelope(xmin, ymin, xmax, ymax); ESRI.ArcGIS.ADF.Web.SpatialFilter spatialFilter = new ESRI.ArcGIS.ADF.Web.SpatialFilter(); spatialFilter.Geometry = adfEnvelope; spatialFilter.ReturnADFGeometries = true; spatialFilter.MaxRecords = 10; System.Data.DataTable queryResultsDataTable = queryFunctionality.Query(gisFunctionality.Name, layerIDs[0], spatialFilter); queryResultsDataTable.TableName = gisResource.Name + "_" + layerNames[0]; for (int i = 0; i < queryResultsDataTable.Rows.Count; i++) { sb.Append("<tr>"); for (int j = 0; j < queryResultsDataTable.Columns.Count; j++) { sb.Append("<td>"); sb.Append(queryResultsDataTable.Rows[i][j].ToString()); sb.Append("</td>"); } sb.Append("</tr>"); } sb.Append("</table>"); } } returnValue = sb.ToString(); } public String GetCallbackResult() { return returnValue; } } } 3.最后结果如下: