ArcGis For Silverlight API,地图显示Gis,绘制点,线,绘制图等(三)--绘制点、线、圆,显示提示信息

平台:Vs 2010,Blend 4,Silverlight 4

调用API: ArcGis for Silverligth API(ESRI.ArcGIS.Client)


OK,今天又有空来写点啦,这个例子自己不想拉的太长了,所以这节多写点东西,我尽量把东西都介绍全面,有不懂的可以留言~

有空大家共同讨论。


好进入正题,如今天标题所示,我们先来看画点,线,圆吧!

        /// <summary>
        /// 绘制界面上的点和线
        /// </summary>
        /// <param name="myMap"></param>
        /// <param name="point"></param>
        /// <param name="pointLine"></param>
        public void DrawAnimationCompleted(Map myMap, List<Graphic> point,ESRI.ArcGIS.Client.Geometry.PointCollection pointLine)
        {
            GraphicsLayer gPointLayer = new GraphicsLayer();
            GraphicsLayer lineLayer = new GraphicsLayer();
            SimpleLineSymbol lineSymbol = new SimpleLineSymbol();
            lineSymbol.Color = new SolidColorBrush(Colors.Brown);
            lineSymbol.Width = 1;
            lineSymbol.Style = SimpleLineSymbol.LineStyle.Solid;

            // 画线到图层上并绘制到地图上
            GisMap.AddLayersToMap(myMap, new GraphicsLayer[] { lineLayer });
            GisLine.DrawLineOnMap(pointLine, lineLayer, lineSymbol);

            GisMap.DrawAllLayers(myMap, new GraphicsLayer[] { gPointLayer }, point);
            GisMap.AddLayersToMap(myMap, new GraphicsLayer[] { gPointLayer });
        }

好,看一下如何画圆吧。

        /// <summary>
        /// 在地图上绘制圆
        /// </summary>
        /// <param name="myMap">地图</param>
        /// <param name="container">绘制容器</param>
        /// <param name="pt">要绘制的点</param>
        /// <param name="drawCircleLayer"></param>
        /// <param name="circleKm">直径</param>
        /// <param name="color">填充色</param>
        /// <param name="ellipseStroke">边框色</param>
        public void DrawEllipse(Map myMap, Canvas container, MapPoint pt,ref ElementLayer drawCircleLayer, double circleKm,Color color,Color ellipseStroke)
        {
            if (!drawCircleLayer.Children.Contains(container))
            {
                drawCircleLayer.Children.Add(container);
                container.Opacity = 0.5;
                container.SetValue(ElementLayer.EnvelopeProperty, new Envelope(myMap.Extent.XMax, myMap.Extent.YMax, myMap.Extent.XMin, myMap.Extent.YMin));
            }

            Point ptFirst = myMap.MapToScreen(new MapPoint(Convert.ToDouble(pt.X),
                Convert.ToDouble(pt.Y)));

            Point pt7 = myMap.MapToScreen(new MapPoint((Convert.ToDouble(pt.X) + circleKm * kmToEN),
                Convert.ToDouble(pt.Y)));

            Ellipse ellipse7 = new Ellipse();
            ellipse7.Width = (pt7.X - ptFirst.X) * 2;
            ellipse7.Height = ellipse7.Width;
            ellipse7.StrokeThickness = 1;
            ellipse7.Stroke = new SolidColorBrush(ellipseStroke);
            ellipse7.Fill = new SolidColorBrush(color);
            Canvas.SetLeft(ellipse7, ptFirst.X - ellipse7.Width / 2);
            Canvas.SetTop(ellipse7, ptFirst.Y - ellipse7.Width / 2);
            ellipse7.Opacity = 0.5;

            container.Children.Add(ellipse7);
            container.IsHitTestVisible = false;
            container.SetValue(Canvas.ZIndexProperty, -10);
        }

这是一个画圆的方法,需要地图类,点,Canvas容器,Gis 的地图层ElementLayer和color

我前台是这样调用的

      /// <summary>
        /// 绘制7级风圈和10级风圈
        /// </summary>
        /// <param name="myMap"></param>
        /// <param name="sender"></param>
        public void DrawEllipse7And10WindCircle(Map myMap, object sender)
        {
                        if (GisMap.LayerExist(myMap, "WindCircleLayer"))           
 {                GisMap.DeleteLayersToMap(myMap, "WindCircleLayer");            }

            ElementLayer circleLayer = new ElementLayer();
            circleLayer.ID = "WindCircleLayer";

            Canvas circleCanvas = new Canvas();

            Graphic tipGraphic = sender as Graphic;

            if (Convert.ToDouble(tipGraphic.Attributes["WindCircle7"]) != 0)
            {
                Color color = new Color();
                color.A = 255;
                color.R = 153;
                color.G = 105;
                color.B = 192;

                DrawEllipse(myMap, circleCanvas, new MapPoint(Convert.ToDouble(tipGraphic.Attributes["Longitude"]),
                    Convert.ToDouble(tipGraphic.Attributes["Latitude"])), ref circleLayer,
                    Convert.ToDouble(300), color, Colors.Blue);

            }

            if (Convert.ToDouble(tipGraphic.Attributes["WindCircle10"]) != 0)
            {
                Color color = new Color();
                color.A = 255;
                color.R = 111;
                color.G = 91;
                color.B = 171;

                this.DrawEllipse(myMap, circleCanvas, new MapPoint(Convert.ToDouble(tipGraphic.Attributes["Longitude"]),
                    Convert.ToDouble(tipGraphic.Attributes["Latitude"])), ref circleLayer,
                    Convert.ToDouble(tipGraphic.Attributes["WindCircle10"]), color, Colors.Blue);
            }


            GisMap.AddLayersToMap(myMap, new ElementLayer[] { circleLayer });
        }

这里的sender是一个Gis元素 Graphic,根据我的WebService 取到的实体后我把这个点加上了Attributes,一系列属性,所以在上面的代码可以看到tipGraphic.Attributes["WindCircle10"],


下面的代码就是在我从WebService取到实体后做添加点的代码:

        /// <summary>
        /// 添加台风点代码
        /// 添加鼠标移入、移出事件
        /// </summary>
        /// <param name="model"></param>
        /// <param name="i"></param>
        private void AddPointToGraphic(TyphoonModel model, int i, List<Graphic> pointParam)
        {
            SimpleMarkerSymbol symbol = new SimpleMarkerSymbol();
            Color color = new Color();
            color.A = 255;
            
            if (Convert.ToDouble(model.WS) <= 17.1)
            {
                color.R = 0;
                color.G = 254;
                color.B = 223;
                symbol.Color = new SolidColorBrush(color);
            }
            else if (Convert.ToDouble(model.WS) > 17.1 && Convert.ToDouble(model.WS) <= 24.4)
            {
                color.R = 254;
                color.G = 243;
                color.B = 0;
                symbol.Color = new SolidColorBrush(color);
            }
            else if (Convert.ToDouble(model.WS) > 24.4 && Convert.ToDouble(model.WS) <= 32.6)
            {
                color.R = 254;
                color.G = 144;
                color.B = 44;
                symbol.Color = new SolidColorBrush(color);
            }
            else if (Convert.ToDouble(model.WS) > 32.6 && Convert.ToDouble(model.WS) <= 41.4)
            {
                color.R = 254;
                color.G = 4;
                color.B = 4; symbol.Color = new SolidColorBrush(color);
            }
            else if (Convert.ToDouble(model.WS) > 41.4 && Convert.ToDouble(model.WS) <= 50.9)
            {
                color.R = 254;
                color.G = 58;
                color.B = 163; symbol.Color = new SolidColorBrush(color);
            }
            else if (Convert.ToDouble(model.WS) > 50.9)
            {
                color.R = 174;
                color.G = 0;
                color.B = 217; symbol.Color = new SolidColorBrush(color);
            }

            symbol.Size = 10;
            if (i == 0)
            {
                symbol.Style = SimpleMarkerSymbol.SimpleMarkerStyle.Square;
            }
            else
            {
                symbol.Style = SimpleMarkerSymbol.SimpleMarkerStyle.Circle;
            }

            pointParam.Add(new Graphic()
            {
                Geometry = new MapPoint(model.Longitude, model.Latitude),
                Symbol = symbol
            });

            pointParam[i].Attributes.Add("TyphoonID", model.TyphoonID);
            pointParam[i].Attributes.Add("TyphoonNo", model.TyphoonNo);
            pointParam[i].Attributes.Add("TyphoonName", model.TyphoonName);
            pointParam[i].Attributes.Add("WindCircle7", model.WindCircle7);
            pointParam[i].Attributes.Add("WindCircle10", model.WindCircle10);
            pointParam[i].Attributes.Add("WS", model.WS);
            pointParam[i].Attributes.Add("Pressure", model.Pressure);
            pointParam[i].Attributes.Add("IssueTime", model.IssueTime);
            pointParam[i].Attributes.Add("Future", model.Future);
            pointParam[i].Attributes.Add("Latitude", model.Latitude);
            pointParam[i].Attributes.Add("Longitude", model.Longitude);
        }


信息提示功能如图:




我们先看下Xmal中的代码:

 

        <Canvas x:Name="typhoonPointInfoCanvas" Visibility="Visible" Height="188" HorizontalAlignment="Left" Margin="0,-272,0,0" VerticalAlignment="Top" Width="360">
            <Path Stretch="Fill" Stroke="Black"  Height="168.5" Width="328.5" UseLayoutRounding="False" Canvas.Left="0.5" Canvas.Top="-0.5" Data="M113,25 C113,11.745166 123.74516,1.0000004 137,1.0000004 L304,1.0000004 C317.25482,1.0000004 328,11.745166 328,25 L328,144 C328,157.25484 317.25482,168 304,168 L137,168 C123.74516,168 113,157.25484 113,144 z M112.5,24.499998 L0.5,0.5 L112.5,72.499992 z" Fill="{StaticResource CommonGradient2}"/>

            <StackPanel Orientation="Vertical" Height="168" Width="176" Canvas.Left="137" Canvas.Top="15">
                <TextBlock x:Name="typhoonNameTextBlock" Height="20" Text="名称:" Foreground="White" TextWrapping="Wrap"/>
                <TextBlock x:Name="typhoonCollectionTimeTextBlock" Height="20" Text="时间:" Foreground="White" TextWrapping="Wrap" d:LayoutOverrides="HorizontalAlignment"/>
                <TextBlock x:Name="typhoonPositionTextBlock" Height="20" Text="位置:" Foreground="White" TextWrapping="Wrap" d:LayoutOverrides="HorizontalAlignment"/>
                <TextBlock x:Name="typhoonWSTextBlock" Height="20" Text="最大风速:" Foreground="White" TextWrapping="Wrap" d:LayoutOverrides="HorizontalAlignment"/>
                <TextBlock x:Name="typhoonPressureTextBlock" Height="20" Text="中心气压:" Foreground="White" TextWrapping="Wrap" d:LayoutOverrides="HorizontalAlignment"/>
                <TextBlock x:Name="typhoonCircle7TextBlock" Height="20" Text="7级风圈半径:" Foreground="White" TextWrapping="Wrap" d:LayoutOverrides="HorizontalAlignment"/>
                <TextBlock x:Name="typhoonCircle10TextBlock" Height="20" Text="10级风圈半径:" Foreground="White" TextWrapping="Wrap" d:LayoutOverrides="HorizontalAlignment"/>

            </StackPanel>
        </Canvas>

        <LinearGradientBrush x:Key="CommonGradient" StartPoint="0.5,0" EndPoint="0.5,1">            <GradientStop Offset="0" Color="#ee76a8d3"/>            <GradientStop Offset="0.25" Color="#ee5b8cb5"/>            <GradientStop Offset="0.75" Color="#ee4b7ba7"/>        </LinearGradientBrush>

 

 

看下c# 中的代码:

当我们添加那些点也就是 Graphic 的时候有这样一个事件MouseEventHandler


 

            // 添加点和线,先显示点层,动画结束后显示线层
            mapDraw.DrawLineAndPoint(ref point, myMap, gLayer, ref pointLine, e, length);


            // 添加点事件
            foreach (Graphic item in point)
            {
                item.MouseEnter += new MouseEventHandler(MainPage_MouseEnter);
                item.MouseLeave += new MouseEventHandler(MainPage_DrawLine);
            }

 

 

 

        /// <summary>
        /// 绘制单条台风动画前的信息
        /// </summary>
        /// <param name="point"></param>
        /// <param name="myMap"></param>
        /// <param name="gLayer"></param>
        /// <param name="pointLine"></param>
        /// <param name="e"></param>
        /// <param name="length"></param>
        public void DrawLineAndPoint(ref List<Graphic> point,Map myMap,GraphicsLayer gLayer,
           ref ESRI.ArcGIS.Client.Geometry.PointCollection pointLine, GetTyphoonsCompletedEventArgs e, int length)
        {
            #region 添加点代码
            point = new List<Graphic>();
            for (int i = 0; i < length; i++)
            {
                AddPointToGraphic(e.Result[i], i, point);
            }
            #endregion

            // 添加线的代码
            pointLine = new ESRI.ArcGIS.Client.Geometry.PointCollection();
            AddLineToMap(e.Result.ToList(), length, pointLine);

            // 显示点层
            GisMap.DrawAllLayers(myMap, new GraphicsLayer[] { gLayer }, point);
            GisMap.AddLayersToMap(myMap, new GraphicsLayer[] { gLayer });
        }

 

AddPointToGraphic这个方法就是图片上面的那段代码
 item.MouseEnter += new MouseEventHandler(MainPage_MouseEnter);     
 item.MouseLeave += new MouseEventHandler(MainPage_DrawLine);  
这两段代码就是我们添加鼠标移入和移出事件了,我们看下移入事件:
        void MainPage_MouseEnter(object sender, MouseEventArgs e)
        {
            Graphic graphic = sender as Graphic;
            Cursor = Cursors.Hand;

            typhoonPointInfoCanvas.Visibility = Visibility.Visible;

            Point pt = myMap.MapToScreen(new MapPoint(Convert.ToDouble(graphic.Attributes["Longitude"]), Convert.ToDouble(graphic.Attributes["Latitude"])));

            typhoonPointInfoCanvas.SetValue(Grid.MarginProperty, new Thickness(pt.X, pt.Y, 0, 0));

            typhoonNameTextBlock.Text = "台风:" + graphic.Attributes["TyphoonName"].ToString();
            typhoonCollectionTimeTextBlock.Text = "时间:" + graphic.Attributes["IssueTime"].ToString();
            typhoonPositionTextBlock.Text = "位置:" + graphic.Attributes["Longitude"].ToString() + "°E," + graphic.Attributes["Latitude"].ToString() + "°N";
            typhoonWSTextBlock.Text = "最大风速:" + graphic.Attributes["WS"].ToString() + " m/s";
            typhoonPressureTextBlock.Text = "中心气压:" + graphic.Attributes["Pressure"].ToString() + " hPa";
            typhoonCircle7TextBlock.Text = "7级风圈半径:" + graphic.Attributes["WindCircle7"].ToString() + " km";
            typhoonCircle10TextBlock.Text = "10级风圈半径:" + graphic.Attributes["WindCircle10"].ToString() + " km";

            circle.DrawEllipse7And10WindCircle(myMap, sender);
            selectedGarphic = sender as Graphic;
        }
我们看到在显示信息的同时我们又把圆画了上去DrawEllipse7And10WindCircle()这个函数

GisMap是个静态类,以下是他的代码

    /// <summary>
    /// ArcGis 调用类
    /// 动态加载、显示隐藏层数据、加载层上的点等
    /// 日期:2010-5-10
    /// 作者:AngelSoft
    /// </summary>
    public static class GisMap
    {

        /// <summary>
        /// 绘制所有的点到地图上
        /// </summary>
        /// <param name="glayer"></param>
        /// <param name="cacheGraphic"></param>
        public static void DrawSymbol(GraphicsLayer glayer, List<Graphic> cacheGraphic)
        {
            if (glayer != null)
            {
                int graphicCount = cacheGraphic.Count;
                for (int i = 0; i < graphicCount; i++)
                {
                    glayer.Graphics.Add(cacheGraphic[i]);
                } // i
            }
        }
        /// <summary>
        /// 加载所有图层上的点
        /// 动态绘制
        /// 图层和点的对应关系要正确
        /// 有几个图层就要有几个点集合
        /// </summary>
        /// <param name="map">ArcGis 地图变量</param>
        /// <param name="layers">GraphicLayer 层数组</param>
        /// <param name="graphicParam">Graphic 点数组</param>
        public static void DrawLayers(Map map, GraphicsLayer[] layers, params List<Graphic>[] graphicParam)
        {
            // 计算要绘制的层数并一层一层的绘制(调用动态绘制方法)
            if (layers != null)
            {
                int length = layers.Length;
                for (int i = 0; i < length; i++)
                {
                    if (layers[i] == null)
                    {
                        layers[i] = new GraphicsLayer();
                    }
                    DynamicDrawSymbol(layers[i], graphicParam[i], map);
                }
            }
        }


        /// <summary>
        /// 加载所有图层上的点
        /// 画所有点
        /// 图层和点的对应关系要正确
        /// 有几个图层就要有几个点集合
        /// </summary>
        /// <param name="map">ArcGis 地图变量</param>
        /// <param name="layers">GraphicLayer 层数组</param>
        /// <param name="graphicParam">Graphic 点数组</param>
        public static void DrawAllLayers(Map map, GraphicsLayer[] layers, params List<Graphic>[] graphicParam)
        {
            // 计算要绘制的层数并一层一层的绘制(调用动态绘制方法)
            if (layers != null)
            {
                int length = layers.Length;
                for (int i = 0; i < length; i++)
                {
                    if (layers[i] == null)
                    {
                        layers[i] = new GraphicsLayer();
                    }
                    DrawAllGraphics(layers[i], graphicParam[i]);
                }
            }
        }



        /// <summary>
        /// 隐藏或显示 ArcGis 层
        /// </summary>
        /// <param name="show">隐藏或显示</param>
        /// <param name="layers">层</param>
        public static void LayersVisibility(bool show, params GraphicsLayer[] layers)
        {
            if (layers != null)
            {
                foreach (GraphicsLayer item in layers)
                {
                    item.Visible = show;
                }
            }
        }


        /// <summary>
        /// 将图层数组全部从 map 中移除
        /// </summary>
        /// <param name="map">表示一张 ArcGis 地图</param>
        /// <param name="layers">表示地图层的数组</param>
        public static void DeleteLayersToMap(Map map, GraphicsLayer[] layers)
        {
            // 逐个将数据移除
            foreach (GraphicsLayer item in layers)
            {
                map.Layers.Remove(item);
            }
        }

        /// <summary>
        /// 根据 ID 号删除某层
        /// </summary>
        /// <param name="map"></param>
        /// <param name="ID"></param>
        /// <returns></returns>
        public static void DeleteLayersToMap(Map map, string[] ID)
        {
            int length = ID.Length;

            for (int i = 0; i < length; i++)
            {
                foreach (Layer item in map.Layers)
                {
                    if (item.ID == ID[i])
                    {
                        map.Layers.Remove(item);
                        length--;
                        break;
                    }
                }
            }
        }

        /// <summary>
        /// 将图层数组全部从 map 中移除
        /// </summary>
        /// <param name="map">表示一张 ArcGis 地图</param>
        /// <param name="layers">表示地图层的数组</param>
        public static void DeleteLayersToMap(Map map, ElementLayer[] layers)
        {
            // 逐个将数据移除
            foreach (ElementLayer item in layers)
            {
                map.Layers.Remove(item);
            }
        }


        /// <summary>
        /// 删除地图上的某一层
        /// </summary>
        /// <param name="myMap"></param>
        /// <param name="ID">ID号</param>
        public static void DeleteLayersToMap(Map myMap, string ID)
        {
            int layers = myMap.Layers.Count;
            for (int i = 0; i < layers; i++)
            {
                if (myMap.Layers[i].ID == ID)
                {
                    myMap.Layers.RemoveAt(i);
                    return;
                }
            }
        }


        public static bool LayerExist(Map myMap, string ID)
        {
            int layers = myMap.Layers.Count;
            for (int i = 0; i < layers; i++)
            {
                if (myMap.Layers[i].ID == ID)
                {
                    return true;
                }
            }
            return false;
        }


        /// <summary>
        /// 将图层数组全部添加到 map 中
        /// </summary>
        /// <param name="map">表示一张 ArcGis 地图</param>
        /// <param name="layers">表示地图层的数组</param>
        public static void AddLayersToMap(Map map, GraphicsLayer[] layers)
        {
            // 逐个将数据添加到当前地图中
            foreach (GraphicsLayer item in layers)
            {
                if (item != null)
                {
                    map.Layers.Add(item);
                }
            }
        }

        /// <summary>
        /// 将图层数组全部添加到 map 中
        /// </summary>
        /// <param name="map">表示一张 ArcGis 地图</param>
        /// <param name="layers">表示地图层的数组</param>
        public static void AddLayersToMap(Map map, ElementLayer[] layers)
        {
            // 逐个将数据添加到当前地图中
            foreach (ElementLayer item in layers)
            {
                map.Layers.Add(item);
            }
        }

        /// <summary>
        /// 绘制所有的点到地图上
        /// </summary>
        /// <param name="eLayer"></param>
        /// <param name="image"></param>
        public static void AddImageToElementLayer(ElementLayer eLayer, List<Image> image)
        {
            if (eLayer != null)
            {
                foreach (Image item in image)
                {
                    eLayer.Children.Add(item);
                }
            }
        }

        /// <summary>
        /// 隐藏或显示 ArcGis 层
        /// </summary>
        /// <param name="show">隐藏或显示</param>
        /// <param name="layers">层</param>
        public static void LayersVisibility(bool show, params ElementLayer[] layers)
        {
            if (layers != null)
            {
                foreach (ElementLayer item in layers)
                {
                    item.Visible = show;
                }
            }
        }

        /// <summary>
        /// 动态加载图层
        /// 使用 ElementLayer 层
        /// </summary>
        /// <param name="eLayer"></param>
        /// <param name="cacheGraphic"></param>
        /// <param name="map"></param>
        public static void DynamicDrawElementLayer(ElementLayer eLayer, List<UIElement> cacheElement, Map map)
        {
            // 以下四个变量分别表示地图的四个边
            // 即最大经纬度和最小经纬度
            // xMax最大经度,yMax最大纬度
            double xMax = map.Extent.XMax + 2;
            double xMin = map.Extent.XMin - 2;
            double yMax = map.Extent.YMax + 2;
            double yMin = map.Extent.YMin - 2;

            // 去除不在坐标范围内的点,先检查图层是否为空
            if (eLayer != null)
            {
                int graphicCount = eLayer.Children.Count;
                for (int i = 0; i < graphicCount; i++)
                {
                    UIElement element = eLayer.Children[i];
                    
                    // 判断经度,纬度
                    if (!(((element.GetValue(ElementLayer.EnvelopeProperty) as Envelope).Extent.XMax < xMax && (element.GetValue(ElementLayer.EnvelopeProperty) as Envelope).Extent.XMax > xMin)
                        && ((element.GetValue(ElementLayer.EnvelopeProperty) as Envelope).Extent.YMax < yMax && (element.GetValue(ElementLayer.EnvelopeProperty) as Envelope).Extent.YMax > yMin)))
                    {
                        // 将点在地图上移除,并放在缓存中
                        cacheElement.Add(eLayer.Children[i]);
                        eLayer.Children.Remove(eLayer.Children[i]);
                        graphicCount--;   // 当从集合中移除元素时应该把 graphicCount 减1
                        i--;              // 元素被移除后相当于当前元素的后一位元素会 -1,应该再循环一次本次循环所以应该 -1
                    }
                } // i
            }

            // 检查缓存是否为空,并将点绘制到图形上
            if (cacheElement != null)
            {
                int count = cacheElement.Count;
                for (int i = 0; i < count; i++)
                {
                    // 判断经度,纬度
                    if (((cacheElement[i].GetValue(ElementLayer.EnvelopeProperty) as Envelope).Extent.XMax < xMax && (cacheElement[i].GetValue(ElementLayer.EnvelopeProperty) as Envelope).Extent.XMax > xMin)
                        && ((cacheElement[i].GetValue(ElementLayer.EnvelopeProperty) as Envelope).Extent.YMax < yMax && (cacheElement[i].GetValue(ElementLayer.EnvelopeProperty) as Envelope).Extent.YMax > yMin))
                    {
                        // 运行到此则该点在目前地图范围内,将该点加入到地图中
                        eLayer.Children.Add(cacheElement[i]);
                        cacheElement.Remove(cacheElement[i]);
                        count--;    // 当从集合中移除元素时应该把 count 减1
                        i--;        // 元素被移除后相当于当前元素的后一位元素会 -1,应该再循环一次本次循环所以应该 -1
                        continue;
                    }
                }
            }
        }

        /// <summary>
        /// 将所有元素画到地图上
        /// </summary>
        /// <param name="eLayer"></param>
        /// <param name="cacheElement"></param>
        public static void DrawAllUIElement(ElementLayer eLayer, List<UIElement> cacheElement)
        {
            if (eLayer != null)
            {
                foreach (UIElement item in cacheElement)
                {
                    eLayer.Children.Add(item);
                }
            }
        }

        /// <summary>
        /// 动态的绘制图层
        /// 当然地图移动到相应的坐标后绘制(保留原来的点,绘制新的数据)
        /// 实现了无刷新绘制
        /// </summary>
        /// <param name="glayer">表示地图上的层</param>
        /// <param name="cacheGraphic">存放 Graphics 的缓存</param>
        /// <param name="map">表示一张 ArcGis 地图</param>
        private static void DynamicDrawSymbol(GraphicsLayer glayer, List<Graphic> cacheGraphic, Map map)
        {
            // 以下四个变量分别表示地图的四个边
            // 即最大经纬度和最小经纬度
            // xMax最大经度,yMax最大纬度
            double xMax = map.Extent.XMax + 2;
            double xMin = map.Extent.XMin - 2;
            double yMax = map.Extent.YMax + 2;
            double yMin = map.Extent.YMin - 2;

            // 去除不在坐标范围内的点,先检查图层是否为空
            if (glayer != null)
            {
                int graphicCount = glayer.Graphics.Count;
                for (int i = 0; i < graphicCount; i++)
                {
                    // 判断经度,纬度
                    if (!((glayer.Graphics[i].Geometry.Extent.XMax < xMax && glayer.Graphics[i].Geometry.Extent.XMax > xMin)
                        && (glayer.Graphics[i].Geometry.Extent.YMax < yMax && glayer.Graphics[i].Geometry.Extent.YMax > yMin)))
                    {
                        // 将点在地图上移除,并放在缓存中
                        cacheGraphic.Add(glayer.Graphics[i]);
                        glayer.Graphics.Remove(glayer.Graphics[i]);
                        graphicCount--;   // 当从集合中移除元素时应该把 graphicCount 减1
                        i--;              // 元素被移除后相当于当前元素的后一位元素会 -1,应该再循环一次本次循环所以应该 -1
                    }
                } // i
            }

            // 检查缓存是否为空,并将点绘制到图形上
            if (cacheGraphic != null)
            {
                int count = cacheGraphic.Count;
                for (int i = 0; i < count; i++)
                {
                    // 判断经度,纬度
                    if ((cacheGraphic[i].Geometry.Extent.XMax < xMax && cacheGraphic[i].Geometry.Extent.XMax > xMin)
                        && (cacheGraphic[i].Geometry.Extent.YMax < yMax && cacheGraphic[i].Geometry.Extent.YMax > yMin))
                    {
                        // 运行到此则该点在目前地图范围内,将该点加入到地图中
                        glayer.Graphics.Add(cacheGraphic[i]);
                        cacheGraphic.Remove(cacheGraphic[i]);
                        count--;    // 当从集合中移除元素时应该把 count 减1
                        i--;        // 元素被移除后相当于当前元素的后一位元素会 -1,应该再循环一次本次循环所以应该 -1
                        continue;
                    }
                }
            }
        }

        /// <summary>
        /// 将所有元素画到地图上
        /// </summary>
        /// <param name="eLayer"></param>
        /// <param name="cacheElement"></param>
        private static void DrawAllGraphics(GraphicsLayer eLayer, List<Graphic> cacheGraphic)
        {
            if (eLayer != null)
            {
                foreach (Graphic item in cacheGraphic)
                {
                    eLayer.Graphics.Add(item);
                }
            }
        }
    }       

 

 

今天把 GisMap 这个类都写出来了也为了我写下一篇文章做准备吧!后面会写一篇动态加载数据点的文章!因为当大批量点(2000)左右加载到地图上的时候,

就会非常的卡,基本都动不了,所以我们要动态去加载这些点。


posted @ 2010-11-05 14:12  Royal_Wh  阅读(5072)  评论(15编辑  收藏  举报