Silverlight API坐标点生成线并量测标注

来源:互联网 发布:淘宝卖东西进货渠道 编辑:程序博客网 时间:2024/06/10 04:40

下面的几行代码就可以实现在silverlight API下向临时图层添加点

 

//生成要添加的点

ESRI.ArcGIS.Client.Geometry.MapPoint pPoint =newMapPoint(Convert.ToDouble(txtXCoor.Text.Trim())

                    , Convert.ToDouble(txtYCoor.Text.Trim()), Map.SpatialReference);

//将生成的点添加到临时图层中

Graphic pGraphic =newGraphic

                {

                   Geometry = pPoint,

                   Symbol = LayoutRoot.Resources["GraphicPointSym"]asESRI.ArcGIS.Client.Symbols.Symbol

                };

GraphicsLayer pGraphicsLayerPoint =Map.Layers["GraphicLayerPoint"]asGraphicsLayer;

pGraphicsLayerPoint.Graphics.Add(pGraphic);

//添加至节点集合

pointColl.Add(pPoint);

从节点集合中取出最近二次添加的点来创建线对象,并添加至临时图层;

                //取出节点集合中最近二次添加的点

                ESRI.ArcGIS.Client.Geometry.PointCollection points =new ESRI.ArcGIS.Client.Geometry.PointCollection();

               points.Add(pointColl[pointColl.Count - 1]);

               points.Add(pointColl[pointColl.Count - 2]);

                //创建线对象

               ESRI.ArcGIS.Client.Geometry.Polyline pPolyline =new ESRI.ArcGIS.Client.Geometry.Polyline();

               pPolyline.Paths.Add(points);

                pPolyline.SpatialReference= Map.SpatialReference;

                //将创建的线对象添加至临时图层

                Graphic pGraphic =newGraphic

                {

                   Geometry = pPolyline,

                   Symbol = LayoutRoot.Resources["GraphicLineSym"]asESRI.ArcGIS.Client.Symbols.Symbol

                };

                GraphicsLayer pGraphicsLayerLine =Map.Layers["GraphicLayerLine"]asGraphicsLayer;

               pGraphicsLayerLine.Graphics.Add(pGraphic);

                IList<Graphic> pGraphicLst =newList<Graphic>();

               pGraphicLst.Add(pGraphic);

 

                //长度信息标注点位

               pLabPoint = pPolyline.Extent.GetCenter();

由XY坐标生成点、线等几何图形时,要记得为生成的几何图形设置空间参考,否则在用GeometryService进行图形操作时会报错。

GeometryService服务

几何服务(GeometryService)提供了一系列的方法在输入的图形上进行几何相关的操作,包括生成缓冲区、计算面积和长度、设置图形投影、简化图形、判断空间位置关系等等

主要功能如下:

² AreasAndLengths

计算多边形的面积和周长,可指定长度和面积单位。

² Buffer

生成缓冲区。

² ConvexHull

计算凸多边形。

² Cut

使用拆分线将输入的线或多边形拆分成两部分。

² Densify

通过在现有节点间增加新的节点来增加几何体节点的密度。

² Difference

构建一组几何体不另外一个几何体的差异部分(丌相交的部分)。

² Distance

计算两个几何体的距离。

² Generalize

为输入的线或面几何体执行Douglas-Poiker概化(减少节点数量)算法。

² Intersect

构建一组几何体不另外一个几何体相交部分,仅迒回集合中几何体相交部分。

² LabelPoints

计算一组Graphic的适合标注的点。

² Lengths

计算线的长度。

² Offset

构建线或多边形的偏秱副本。

² Project

将几何体投影到新的空间参考下。

² Reshape

使用重塑线重塑线或多边形形状。

² Simplify

修改给定的几何体,以使之从拓扑上满足几何体类型的定义。

² TrimExtend

对每个输入的线执行截断或延伸操作,以使之匹配另外一条线。

² Union

将一组几何体合幵,所有输入的几何体必项是同一类型。

² AutoComplete

自劢完成。

 

下面介绍如和通过几何服务计算线对象长度并进行标注

 

//创建几何服务

GeometryService geometryService1=newGeometryService("http://lingy/ArcGIS/rest/services/Geometry/GeometryServer");

geometryService1.LengthsCompleted +=newEventHandler<LengthsEventArgs>(geometryService1_LengthsCompleted);

geometryService1.Failed +=newEventHandler<TaskFailedEventArgs>(geometryService1_Failed);

 

//调用几何服务计算线长度

geometryService1.LengthsAsync(pGraphicLst,LinearUnit.Meter,true, null);

在上行代码中参数pGraphicLst是包含线对象Graphic的List集合;LinearUnit.Meter表示以米为单位返回测量结果

 

在量测结果处理事件中获得测量长度,并进行标注

        void geometryService1_LengthsCompleted(object sender,LengthsEventArgs e)

        {

            try

            {

                //返回的测量长度

                double dvalue = e.Results[0];

                string stext ="长度:" + dvalue.ToString(".###") + "";

 

                //定义标注样式

               ESRI.ArcGIS.Client.Symbols.TextSymbol pTextSymbol =new ESRI.ArcGIS.Client.Symbols.TextSymbol();

               pTextSymbol.FontSize = 10;

               pTextSymbol.Foreground =newSolidColorBrush(Colors.Black);

               pTextSymbol.Text = stext;

 

                //生成标注的Graphic,并添加到临时图层

                Graphic pGraphic =newGraphic

                {

                   Geometry = pLabPoint,

                   Symbol = pTextSymbol

                };

                GraphicsLayer pGraphicsLayerLine =Map.Layers["GraphicLayerLine"]asGraphicsLayer;

               pGraphicsLayerLine.Graphics.Add(pGraphic);

            }

            catch { }

        }

 

原创粉丝点击