zoukankan      html  css  js  c++  java
  • ArcGIS.Server.9.2.DotNet实现动态设置Label标注功能

    目的:
    1.arcgis server9.2 ADF实现动态设置Label标注功能,选择不同图层然后选择要作为Label显示的字段然后更新地图的Label。
    准备工作:
    1.用ArcGis Server Manager或者ArcCatalog发布一个叫usa的Map Service,并且把这个Service启动起来。
    完成后的效果图:


    开始:
    1.新建名为LabelFieldSamples的ASP.NET Web应用程序,在页面上添加MapResourceManager1、Map1控件。
    2.为MapResourceManager1控件添加MapResourceItem,由上到下分别为,(usa的Map Service)名称为:worldCities、DataSourceType:ArcGIS Server Local。
    3.按着上图设置好Map1控件,在页面的右边添加2个<div id="layer"></div>和<div id="field"></div>用来显示图层选择列表和字段选择列表,加一个input 的按钮并且添加onclick事件用来实现按钮功能。具体html代码如下:
    1图层:<div id="layer"></div>
    2<br />
    3字段:<div id="field"></div>
    4<br />
    5<input id="Button1" type="button" value="显示Label" onclick="showLabel()" />
    4.界面工作完成后接下来首先完成图层列表选择和字段列表选择的功能,这里采用Ajax的无刷新方式,和以前所有的例子一样页面类实现ICallbackEventHandler的接口,具体代码如下:
     1public partial class _Default : System.Web.UI.Page, ICallbackEventHandler
     2    {
     3        //脚本段字符串
     4        public string m_Callback;
     5        protected void Page_Load(object sender, EventArgs e)
     6        {
     7            //生成脚本段字符串用来供客户端的js调用
     8            m_Callback = Page.ClientScript.GetCallbackEventReference(Page, "argument""processCallbackResult""context""processCallbackError"true);
     9        }

    10
    11        ICallbackEventHandler 成员
    25
    26        private string RaiseCallbackEvent(string _callbackArg)
    27        {
    28            string v = "";
    29            //代码待写
    30            return v;
    31        }

    32}
    5.接下来页面切换到Html视图,要在页面载入的时候需要把图层列表取来进行显示所以在需要在body标签类添加onload函数:<body onload="pageLoad()">,这样在页面载入的时候就会执行pageLoad()的方法,接下来在head标签内添加pageLoad()方法,代码如下:
    1<script type="text/javascript" language="javascript">
    2    function pageLoad()
    3    {
    4       var argument = "ControlID=Map1&ControlType=Map&Type=Layer";
    5       var context = "Map";
    6       var rv=<%= m_Callback %>;
    7       eval(rv);
    8    }

    9</script>
    6.这样当页面载入的时候会执行pageLoad()的方法想服务端请求获取图层列表,接下在需要在cs代码端处理这个获取图层列表的请求,切换到源代码视图修改private string RaiseCallbackEvent(string _callbackArg)这个方法的代码如下:
     1private string RaiseCallbackEvent(string _callbackArg)
     2        {
     3            string v = "";
     4            NameValueCollection keyValColl = CallbackUtility.ParseStringIntoNameValueCollection(_callbackArg);
     5            //获取图层列表
     6            if (keyValColl["Type"].ToString() == "Layer")
     7            {
     8                v = getLayerResult();
     9            }

    10          //处理结果返回
    11            return v;
    12        }

    13//获取图层列表显示
    14private string getLayerResult()
    15        {
    16
    17            string[] lnames = getLayer();
    18            string str = "";
    19            string str2 = "";
    20            for (int i = 0; i < lnames.Length; i++)
    21            {
    22                str = str + "<option value='" + lnames[i] + "'>" + lnames[i] + "</option>";
    23            }

    24            string[] fields = getFields(0);
    25            for (int j = 0; j < fields.Length; j++)
    26            {
    27                str2 = str2 + "<option value='" + fields[j] + "'>" + fields[j] + "</option>";
    28            }

    29            str = "<select id='layer' onchange='changeLayer(this)'>" + str + "</select>";
    30            str2 = "<select id='field'>" + str2 + "</select>";
    31            System.Text.StringBuilder sb = new System.Text.StringBuilder();
    32            sb.AppendFormat("div:::{0}:::content:::{1}^^^div:::{2}:::content:::{3}""layer", str, "field", str2);
    33            return sb.ToString();
    34        }
    7.上面的getLayerResult()的方法实现图层列表的获取,以及默认选中图层(index为0图层)的字段列表的获取。在生成客户端的图层选择列表的时候为select添加一个onchange事件用来当选择图层发生变换时同时更新字段选择列表。
    8.切换到html视图添加图层select标签onchange事件changeLayer(this)方法,代码如下:
    1function changeLayer(obj)
    2    {
    3       var argument = "ControlID=Map1&ControlType=Map&Type=ChangeLayer&Args="+obj.value;
    4       var context = "Map";
    5       var rv=<%= m_Callback %>;
    6       eval(rv);
    7    }
    9.同pageLoad()脚本方法一下需要在服务端处理changeLayer(this)方法发起的请求,切换到源代码视图继续修改private string RaiseCallbackEvent(string _callbackArg)这个方法,添加根据图层名称获取字段列表的功能:
    1
    2//更换图层时获取字段列表
    3            else if (keyValColl["Type"].ToString() == "ChangeLayer")
    4            {
    5                string layer = keyValColl["Args"].ToString();
    6                v = getFieldResult(layer);
    7            }

    8
    9
     1//根据图层名获取字段列表显示
     2        private string getFieldResult(string layer)
     3        {
     4            string str2 = "";
     5            string[] fields = getFields(layer);
     6            for (int j = 0; j < fields.Length; j++)
     7            {
     8                str2 = str2 + "<option value='" + fields[j] + "'>" + fields[j] + "</option>";
     9            }

    10            str2 = "<select id='field'>" + str2 + "</select>";
    11            System.Text.StringBuilder sb = new System.Text.StringBuilder();
    12            sb.AppendFormat("div:::{0}:::content:::{1}""field", str2);
    13            return sb.ToString();
    14        }
    10.这样就完成了图层选择列表和字段选择列表的功能,继续来实现显示Label标注的功能。切换到HTML视图添加按钮onclick事件方法showLabel(),代码如下:
     1function showLabel()
     2    {
     3       var argument = "ControlID=Map1&ControlType=Map&Type=Label&Layer="+document.getElementById("layer").value+"&Field="+document.getElementById("field").value;
     4       var context = "Map";
     5       var rv=<%= m_Callback %>;
     6       eval(rv);
     7    }

     8function processCallbackError()
     9    {
    10       alert("出错啦!");
    11    }
    11.然后继续切换到源代码视图在cs段处理showLabel()的请求实现Label功能,修改private string RaiseCallbackEvent(string _callbackArg)这个方法,添加标注功能。
    Code
     1//显示Label
     2        private string showLabel(string layerName,string field)
     3        {
     4            ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality mapFunct = Map1.GetFunctionality("worldCities");
     5            MapResourceLocal mapResLocal = mapFunct.Resource as ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal;
     6            ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapFunctionality mf = mapFunct as ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapFunctionality;
     7            ESRI.ArcGIS.ADF.ArcGISServer.MapDescription mapDesc = mf.MapDescription;
     8            IServerContext serverContext = mapResLocal.ServerContextInfo.ServerContext;
     9            IMapServer mapServer = serverContext.ServerObject as IMapServer;
    10            IMapServerObjects mapServerObjects = mapServer as IMapServerObjects;
    11            int index = getLyaerIndex(layerName);
    12            ILayer layer = mapServerObjects.get_Layer(mapServer.DefaultMapName, index);
    13            //获取面图层
    14            IFeatureLayer pFLayer = layer as IFeatureLayer;
    15
    16            IGeoFeatureLayer pGeoFeatureLayer = pFLayer as IGeoFeatureLayer;
    17            //清除原来的标注属性
    18            pGeoFeatureLayer.AnnotationProperties.Clear();
    19            //标注属性集合
    20            IAnnotateLayerPropertiesCollection pAnnoLayerPropsColl = pGeoFeatureLayer.AnnotationProperties;
    21            ILabelEngineLayerProperties pLabelEngine;
    22
    23            pLabelEngine = serverContext.CreateObject("esriCarto.LabelEngineLayerProperties"as ILabelEngineLayerProperties;
    24
    25            //可以对输出的文字样式进行设置
    26            //ESRI.ArcGIS.Display.ITextSymbol textSymbol = new ESRI.ArcGIS.Display.TextSymbolClass();
    27            pLabelEngine.Symbol.Font = ESRI.ArcGIS.ADF.Converter.ToStdFont(new Font("Times New Roman"14, FontStyle.Italic));
    28            pLabelEngine.Symbol.Color = ESRI.ArcGIS.ADF.Web.UI.WebControls.Converter.ToRGBColor(serverContext, System.Drawing.Color.Blue); 
    29
    30
    31            pLabelEngine.Expression = "[" + field + "]";//设置作为Label的字段名
    32            IAnnotateLayerProperties pAnnoLayerProps = pLabelEngine as IAnnotateLayerProperties;
    33            pAnnoLayerPropsColl.Add(pAnnoLayerProps);
    34            //设置标注显示
    35            pGeoFeatureLayer.DisplayAnnotation = true;
    36            ESRI.ArcGIS.ADF.ArcGISServer.LayerDescription LayerDesc = mapDesc.LayerDescriptions[0as ESRI.ArcGIS.ADF.ArcGISServer.LayerDescription;
    37            //设置Label显示
    38            LayerDesc.ShowLabels = true;
    39            //刷新输出结果用来给客户端更新显示
    40            Map1.Refresh();
    41            return Map1.CallbackResults.ToString();
    42        }
    12.其它几个上面有用到的方法代码如下:
     1//根据图层名称获取图层的index
     2        private int getLyaerIndex(string layer)
     3        {
     4            ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality mapFunct = Map1.GetFunctionality("worldCities");
     5            IGISResource gisResource = mapFunct.Resource;
     6            ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality qfunc = (ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality)gisResource.CreateFunctionality(typeof(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality), null);
     7            string[] lids;
     8            string[] lnames;
     9            qfunc.GetQueryableLayers(nullout lids, out lnames);
    10            int index = 0;
    11            for (int i = 0; i < lnames.Length; i++)
    12            {
    13                if (lnames[i] == layer)
    14                {
    15                    index = i;
    16                    break;
    17                }

    18            }

    19            return index;
    20        }

    21//获取图层
    22        private string[] getLayer()
    23        {
    24            ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality mapFunct = Map1.GetFunctionality("worldCities");
    25            IGISResource gisResource = mapFunct.Resource;
    26            ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality qfunc = (ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality)gisResource.CreateFunctionality(typeof(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality), null);
    27            string[] lids;
    28            string[] lnames;
    29            qfunc.GetQueryableLayers(nullout lids, out lnames);
    30            return lnames;
    31        }

    32
    33        //根据图层index获取字段
    34        private string[] getFields(int layerIndex)
    35        {
    36            ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality mapFunct = Map1.GetFunctionality("worldCities");
    37            IGISResource gisResource = mapFunct.Resource;
    38            ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality qfunc = (ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality)gisResource.CreateFunctionality(typeof(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality), null);
    39            string[] lids;
    40            string[] lnames;
    41            qfunc.GetQueryableLayers(nullout lids, out lnames);
    42            string[] fields = qfunc.GetFields(null, lids[layerIndex]);
    43            return fields;
    44        }

    45
    46        //根据图层名获取字段
    47        private string[] getFields(string layer)
    48        {
    49            ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality mapFunct = Map1.GetFunctionality("worldCities");
    50            IGISResource gisResource = mapFunct.Resource;
    51            ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality qfunc = (ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality)gisResource.CreateFunctionality(typeof(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality), null);
    52            string[] lids;
    53            string[] lnames;
    54            qfunc.GetQueryableLayers(nullout lids, out lnames);
    55            int index = 0;
    56            for (int i = 0; i < lnames.Length; i++)
    57            {
    58                if (lnames[i] == layer)
    59                {
    60                    index = i;
    61                    break;
    62                }

    63            }

    64            string[] fields = qfunc.GetFields(null, lids[index]);
    65            return fields;
    66        }

    13.运行查看效果,选择图层然后选择字段,点击按钮就会动态的更新Label的显示。
  • 相关阅读:
    Windows常用内容渗透命令
    字符串编码小记
    卡巴斯基命令解析
    聚合与继承
    Maven与Eclipse整合
    maven系列三:Maven核心概念
    使用Maven构建项目
    Maven项目构建过程练习
    maven系列二:Maven入门
    使用Maven构建多模块项目
  • 原文地址:https://www.cnblogs.com/hll2008/p/1303125.html
Copyright © 2011-2022 走看看