高亮显示要素的另一种方法
高亮显示要素一般可以通过两种方式来实现。一是在地图资源管理器中增加一GraphicsLayer,然后在该图层中将要素重新画一次,另一种方式是设置资源绘图功能的MapDescription属性的CustomGraphic属性。
由于第一种方式使用的较多,在此将介绍第二种方式的实现。
在使用ArcGIS Server绘图功能时,MapDescription属性提供了一个值对象,通过该对象可以修改由ArcGIS Server地图服务生成的地图的显示与内容。
主要的代码如下:
// 高亮显示的主体函数
public static void HighLightShow(Map map, DataTableCollection dtc)
{
MapFunctionality mf = (MapFunctionality)map.GetFunctionality("NorthAmericaMap");
MapDescription mapDescription = mf.MapDescription;
mapDescription.CustomGraphics = null;
SimpleFillSymbol sfs = CreateSimpleFillSymbol();
foreach(DataTable dt in dtc)
{
if (dt.Rows.Count == 0)
continue;
HighLightPolygon(mapDescription, dt, sfs);
}
RefreshMap(map, "NorthAmericaMap");
}
//HighLightShow方法调用到的函数如下
public static SimpleFillSymbol CreateSimpleFillSymbol()
{
ESRI.ArcGIS.ADF.ArcGISServer.RgbColor rgb;
rgb = new ESRI.ArcGIS.ADF.ArcGISServer.RgbColor();
rgb.Red = 255;
rgb.Green = 0;
rgb.Blue = 0;
rgb.AlphaValue = 255;
SimpleLineSymbol lineSym = new SimpleLineSymbol();
lineSym.Color = rgb;
lineSym.Width = 1.0;
lineSym.Style = esriSimpleLineStyle.esriSLSSolid;
SimpleFillSymbol sfs;
sfs = new SimpleFillSymbol();
sfs.Style = esriSimpleFillStyle.esriSFSForwardDiagonal;
sfs.Color = rgb;
sfs.Outline = lineSym;
return sfs;
}
public static void HighLightPolygon(MapDescription mapDescription, DataTable datatable, SimpleFillSymbol sfs)
{
int hasCount = 0;
if (mapDescription.CustomGraphics != null)
hasCount = mapDescription.CustomGraphics.Length;
ESRI.ArcGIS.ADF.ArcGISServer.GraphicElement[] ges;
ges = new GraphicElement[hasCount + datatable.Rows.Count];
CopyCustomGraphics(ges, mapDescription);
int geoIndex = GeometryFieldIndex(datatable);
for (int i = 0; i < datatable.Rows.Count; i++)
{
ESRI.ArcGIS.ADF.Web.Geometry.Polygon polygon = datatable.Rows[i][geoIndex] as ESRI.ArcGIS.ADF.Web.Geometry.Polygon;
PolygonN ags_map_polyn;
ags_map_polyn = ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.Converter.FromAdfPolygon(polygon);
PolygonElement polyelement;
polyelement = new PolygonElement();
polyelement.Symbol = sfs;
polyelement.Polygon = ags_map_polyn;
ges[hasCount + i] = polyelement;
}
mapDescription.CustomGraphics = ges;
}
public static void RefreshMap(Map map, string resourceName)
{
if (map.ImageBlendingMode == ImageBlendingMode.WebTier)
{
map.Refresh();
}
else if (map.ImageBlendingMode == ImageBlendingMode.Browser)
{
map.RefreshResource(resourceName);
}
}
public static void CopyCustomGraphics(GraphicElement[] ges, MapDescription mapDescription)
{
if (mapDescription.CustomGraphics != null)
{
for (int i = 0; i < mapDescription.CustomGraphics.Length; i++)
ges[i] = mapDescription.CustomGraphics[i];
}
}
public static int GeometryFieldIndex(DataTable datatable)
{
int geoIndex = -1;
for (int i = 0; i < datatable.Columns.Count; i++)
{
if (datatable.Columns[i].DataType == typeof(ESRI.ArcGIS.ADF.Web.Geometry.Geometry))
{
// 找到Geometry字段的序号
geoIndex = i;
break;
}
}
return geoIndex;
}
//调用HighLightShow方法来显示查询结果:
public static void ShowIdentifyResult(Map map, DataTableCollection dtc)
{
string returnstring = string.Empty;
foreach (DataTable dt in dtc)
{
if (dt.Rows.Count == 0)
continue;
returnstring += GetHtmlFromDataTable(dt);
}
HighLightShow(map, dtc);
returnstring = returnstring.Replace(""r"n", "");
returnstring = returnstring.Replace(""n", "");
string functionValue = "var theForm = document.forms[0];";
functionValue += "theForm.FunctionValue.Value='" + returnstring + "';";
functionValue += "open('IdentifyResult.htm', 'IdentifyResult');";
AddJavaScriptCallback(map, functionValue);
}
//ShowIdentifyResult中调用到的函数
public static void AddJavaScriptCallback(Map map, string executeString)
{
object[] oa = new object[1];
oa[0] = executeString;
CallbackResult cr = new CallbackResult(null, null, "javascript", oa);
map.CallbackResults.Add(cr);
}
public static string GetHtmlFromDataTable(DataTable dt)
{
GridView gd = new GridView();
gd.ToolTip = dt.TableName;
gd.Caption = dt.TableName;
gd.DataSource = dt;
gd.DataBind();
gd.Visible = true;
gd.BorderWidth = 0;
gd.CssClass = "list-line";
gd.CellPadding = 3;
gd.CellSpacing = 1;
gd.HeaderStyle.CssClass = "barbg";
gd.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
gd.RowStyle.CssClass = "listbg";
string returnString = string.Empty;
using (System.IO.StringWriter sw = new System.IO.StringWriter())
{
HtmlTextWriter htw = new HtmlTextWriter(sw);
gd.RenderControl(htw);
htw.Flush();
string tempStr = sw.ToString();
returnString += tempStr;
}
return returnString;
}
//查询函数,调用ShowIdentifyResult获得查询结果
public static void Identify(Map map, ESRI.ArcGIS.ADF.Web.Geometry.Geometry mapGeometry)
{
IGISFunctionality gisfunc = map.GetFunctionality("NorthAmericaMap");
if (gisfunc == null)
return;
IGISResource gisresource = gisfunc.Resource;
bool supportquery = gisresource.SupportsFunctionality(typeof(IQueryFunctionality));
if (!supportquery)
return;
IQueryFunctionality qfunc;
qfunc = gisresource.CreateFunctionality(typeof(IQueryFunctionality), null) as IQueryFunctionality;
string[] lIDs, lNames;
qfunc.GetQueryableLayers(null, out lIDs, out lNames);
ESRI.ArcGIS.ADF.Web.SpatialFilter spatialfilter = new ESRI.ArcGIS.ADF.Web.SpatialFilter();
spatialfilter.ReturnADFGeometries = false;
spatialfilter.MaxRecords = 1000;
spatialfilter.Geometry = mapGeometry;
System.Data.DataSet dataset = new System.Data.DataSet();
for (int i = 0; i < lIDs.Length; i++)
{
System.Data.DataTable datatable = qfunc.Query(null, lIDs[i], spatialfilter);
if (datatable == null)
continue;
datatable.TableName = lNames[i];
dataset.Tables.Add(datatable);
}
DataTableCollection dtc = dataset.Tables;
ShowIdentifyResult(map, dtc);
}