微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ArcGIS API for Silverlight 解决众多密集点分层显示

问题提出:

        在实际中通常会遇到这样的情况,如果地图范围小,而且需要在地图上展示的元素又比较多的时候(在展现元素符号的同时,还展示元素名称或其他属性值等),这样如果在首次加载地图的时候一次性全部显示,必然会出现严重的重叠现象,怎么解决


解决思路:

      我想大家首先想到的就是分层显示,不错,其实这就是一个方便可行的思路。为数据库表中增加一个显示层次的字段,比如叫ShowLevel,认是0-第一层显示,1-第二层显示...,然后在Map的Layers中添加多个Graphics,有几层就设置多少个,然后在后台程序中通过判断元素是ShowLevel属性,将元素添加到各自不同的Graphics当中去,最后处理Map的ExtendChanged事件,找到一个显示的边界值,然后通过判断当前的缩放比例是否达到了这个边界值,如果达到了,就显示第二层,如果没有达到就隐藏。


具体实现及部分代码如下:

 1、首先数据库增加一个字段,ShowLevel,认值为0

     

2、在MainPage.xaml中多增加几个Graphics

       

  <esri:Map.Layers>
                <esri:Graphicslayer ID="MyGraphicslayer">
                </esri:Graphicslayer>
                <esri:Graphicslayer ID="MyGraphicslayer3">
                </esri:Graphicslayer>
                <!--站点名称-->
                <esri:Graphicslayer ID="Graphicslayer1">
                </esri:Graphicslayer>
                <esri:Graphicslayer ID="Graphicslayer11">
                </esri:Graphicslayer>
                <!--站点数据-->
                <esri:Graphicslayer ID="Graphicslayer2">
                </esri:Graphicslayer>
                <esri:Graphicslayer ID="Graphicslayer22">
                </esri:Graphicslayer>
                <!--站点编码-->
                <esri:Graphicslayer ID="Graphicslayer3">
                </esri:Graphicslayer>
                <esri:Graphicslayer ID="Graphicslayer33">
   </esri:Map.Layers>


3、MainPage.xaml.cs中根据ShowLevel字段值,分别添加到各自的Graphics中

            ESRI.ArcGIS.Client.Projection.WebMercator mercator = new ESRI.ArcGIS.Client.Projection.WebMercator();
            //每次加载时先清空地图上数据
            Graphicslayer graphicslayer = myMap.Layers["MyGraphicslayer"] as Graphicslayer;
            graphicslayer.Cleargraphics();
            Graphicslayer graphicslayer0 = myMap.Layers["MyGraphicslayer3"] as Graphicslayer;
            graphicslayer0.Cleargraphics();


            Graphicslayer graphicslayer1 = myMap.Layers["Graphicslayer1"] as Graphicslayer;
            graphicslayer1.Cleargraphics();
            Graphicslayer graphicslayer2 = myMap.Layers["Graphicslayer2"] as Graphicslayer;
            graphicslayer2.Cleargraphics();
            Graphicslayer graphicslayer3 = myMap.Layers["Graphicslayer3"] as Graphicslayer;
            graphicslayer3.Cleargraphics();


            Graphicslayer graphicslayer11 = myMap.Layers["Graphicslayer11"] as Graphicslayer;
            graphicslayer11.Cleargraphics();
            Graphicslayer graphicslayer22 = myMap.Layers["Graphicslayer22"] as Graphicslayer;
            graphicslayer22.Cleargraphics();
            Graphicslayer graphicslayer33 = myMap.Layers["Graphicslayer33"] as Graphicslayer;
            graphicslayer33.Cleargraphics();


            //获取到所有的山洪雨量点
            ObservableCollection<RainFall> lists = e.Result;


            //从集合中找出最大值对应的经纬度坐标
            RainFall max = lists.OrderByDescending(s => s.YL24).FirstOrDefault();
            //动态添加点到地图上
            Graphic graphic = null; //第一层
            Graphic graphic2 = null; //第二层
            foreach (RainFall item in lists)
            {
                if (!string.IsNullOrEmpty(item.Latitute.ToString()) && !string.IsNullOrEmpty(item.Longitute.ToString()))
                {
                    //有坐标值时,将该监测点添加到地图上去
                    if (max.YLZBM == item.YLZBM)
                    {
                        if (max.YL24 != 0)
                        {
                            //最大值不为0,红点闪烁
                            graphic = new Graphic()
                            {
                                Geometry = mercator.FromGeographic(new MapPoint(double.Parse(item.Latitute.ToString().Trim()),double.Parse(item.Longitute.ToString().Trim()))),Symbol = LayoutRoot.Resources["DefaultMarkerSymbol"] as Symbol
                            };
                        }
                        else
                        {
                            //保持原来的蓝色标记点符号
                            graphic = new Graphic()
                            {
                                Geometry = mercator.FromGeographic(new MapPoint(double.Parse(item.Latitute.ToString().Trim()),Symbol = LayoutRoot.Resources["BlueMarkerSymbol"] as Symbol
                            };
                        }
                        //保存属性
                        graphic.Attributes["YLZBM"] = item.YLZBM; //雨量站编码
                        graphic.Attributes["YLZMC"] = item.ZDMC; //雨量站名称
                        graphic.Attributes["YL24"] = item.YL24; //24小时雨量
                        graphic.Attributes["DTNow"] = item.DTNow; //当前时间
                        graphic.Attributes["Latitute"] = item.Latitute; //纬度
                        graphic.Attributes["Longitute"] = item.Longitute; //经度
                        //将该Graphics添加到Graphicslayer中去
                        graphicslayer.Graphics.Add(graphic);
                        graphicslayer.Opacity = 1;


                        //鼠标移入事件
                        graphic.MouseEnter += new MouseEventHandler(sh_graphic_MouseEnter);
                        graphic.MouseLeave += new MouseEventHandler(sh_graphic_MouseLeave);
                        //鼠标点击事件


                        #region 站点名称


                        //动态添加文本
                        TextSymbol textSymbol = new TextSymbol()
                        {
                            FontFamily = new System.Windows.Media.FontFamily("Microsoft YaHei"),Foreground = new System.Windows.Media.solidColorBrush(Color.FromArgb(255,117,20,99)),FontSize = 12,Text = item.ZDMC,OffsetX = 12,OffsetY = -5
                        };


                        Graphic graphicText1 = new Graphic()
                        {
                            Geometry = mercator.FromGeographic(new MapPoint(double.Parse(item.Latitute.ToString().Trim(),System.Globalization.CultureInfo.InvariantCulture),double.Parse(item.Longitute.ToString().Trim(),System.Globalization.CultureInfo.InvariantCulture))),Symbol = textSymbol
                        };


                        graphicText1.Attributes["TextYLZMC"] = item.ZDMC;
                        graphicslayer1.Graphics.Add(graphicText1);


                        #endregion


                        #region 水位/雨量 数值


                        if (item.YL24 != 0)
                        {
                            TextSymbol textSymbol2 = new TextSymbol()
                            {
                                FontFamily = new System.Windows.Media.FontFamily("Microsoft YaHei"),255,0)),FontSize = 14,Text = item.YL24.ToString(),OffsetX = 10,OffsetY = 23
                            };


                            Graphic graphicText2 = new Graphic()
                            {
                                Geometry = mercator.FromGeographic(new MapPoint(double.Parse(item.Latitute.ToString().Trim(),Symbol = textSymbol2
                            };
                            graphicText2.Attributes["TextYL"] = item.YL24;
                            graphicslayer2.Graphics.Add(graphicText2);
                        }


                        #endregion


                        #region 站点编码


                        //动态添加站点编码
                        TextSymbol textSymbol3 = new TextSymbol()
                        {
                            FontFamily = new System.Windows.Media.FontFamily("Microsoft YaHei"),Text = item.YLZBM,OffsetX = 27,OffsetY = 23
                        };


                        Graphic graphicText3 = new Graphic()
                        {
                            Geometry = mercator.FromGeographic(new MapPoint(double.Parse(item.Latitute.ToString().Trim(),Symbol = textSymbol3
                        };


                        graphicText3.Attributes["TextYLZBM"] = item.YLZBM;
                        graphicslayer3.Graphics.Add(graphicText3);
                        graphicslayer3.Visible = false;


                        #endregion


                        //左键点击事件
                        graphic.MouseLeftButtonDown += new MouseButtonEventHandler(sh_graphic_MouseLeftButtonDown);
                        graphic.MouseLeftButtonUp += new MouseButtonEventHandler(sh_graphic_MouseLeftButtonUp);
                    }
                    else
                    {
                        //这里判断显示层数
                        if (item.ShowLevel == 0)
                        {
                            #region 显示第一层数据


                            graphic = new Graphic()
                            {
                                Geometry = mercator.FromGeographic(new MapPoint(double.Parse(item.Latitute.ToString().Trim()),Symbol = LayoutRoot.Resources["BlueMarkerSymbol"] as Symbol
                            };
                            //保存属性
                            graphic.Attributes["YLZBM"] = item.YLZBM; //雨量站编码
                            graphic.Attributes["YLZMC"] = item.ZDMC; //雨量站名称
                            graphic.Attributes["YL24"] = item.YL24; //24小时雨量
                            graphic.Attributes["DTNow"] = item.DTNow; //当前时间
                            graphic.Attributes["Latitute"] = item.Latitute; //纬度
                            graphic.Attributes["Longitute"] = item.Longitute; //经度
                            //将该Graphics添加到Graphicslayer中去
                            graphicslayer.Graphics.Add(graphic);
                            graphicslayer.Opacity = 1;


                            //鼠标移入事件
                            graphic.MouseEnter += new MouseEventHandler(sh_graphic_MouseEnter);
                            graphic.MouseLeave += new MouseEventHandler(sh_graphic_MouseLeave);
                            //鼠标点击事件


                            #region 站点名称


                            //动态添加文本
                            TextSymbol textSymbol = new TextSymbol()
                            {
                                FontFamily = new System.Windows.Media.FontFamily("Microsoft YaHei"),OffsetY = -5
                            };


                            Graphic graphicText1 = new Graphic()
                            {
                                Geometry = mercator.FromGeographic(new MapPoint(double.Parse(item.Latitute.ToString().Trim(),Symbol = textSymbol
                            };


                            graphicText1.Attributes["TextYLZMC"] = item.ZDMC;
                            graphicslayer1.Graphics.Add(graphicText1);


                            #endregion


                            #region 水位/雨量 数值


                            if (item.YL24 != 0)
                            {
                                TextSymbol textSymbol2 = new TextSymbol()
                                {
                                    FontFamily = new System.Windows.Media.FontFamily("Microsoft YaHei"),OffsetY = 23
                                };


                                Graphic graphicText2 = new Graphic()
                                {
                                    Geometry = mercator.FromGeographic(new MapPoint(double.Parse(item.Latitute.ToString().Trim(),Symbol = textSymbol2
                                };
                                graphicText2.Attributes["TextYL"] = item.YL24;
                                graphicslayer2.Graphics.Add(graphicText2);
                            }


                            #endregion


                            #region 站点编码


                            //动态添加站点编码
                            TextSymbol textSymbol3 = new TextSymbol()
                            {
                                FontFamily = new System.Windows.Media.FontFamily("Microsoft YaHei"),OffsetY = 23
                            };


                            Graphic graphicText3 = new Graphic()
                            {
                                Geometry = mercator.FromGeographic(new MapPoint(double.Parse(item.Latitute.ToString().Trim(),Symbol = textSymbol3
                            };


                            graphicText3.Attributes["TextYLZBM"] = item.YLZBM;
                            graphicslayer3.Graphics.Add(graphicText3);
                            graphicslayer3.Visible = false;


                            #endregion


                            //左键点击事件
                            graphic.MouseLeftButtonDown += new MouseButtonEventHandler(sh_graphic_MouseLeftButtonDown);
                            graphic.MouseLeftButtonUp += new MouseButtonEventHandler(sh_graphic_MouseLeftButtonUp);


                            #endregion
                        }
                        else
                        {
                            #region 显示第二层数据


                            graphic2 = new Graphic()
                            {
                                Geometry = mercator.FromGeographic(new MapPoint(double.Parse(item.Latitute.ToString().Trim()),Symbol = LayoutRoot.Resources["BlueMarkerSymbol"] as Symbol
                            };
                            //保存属性
                            graphic2.Attributes["YLZBM"] = item.YLZBM; //雨量站编码
                            graphic2.Attributes["YLZMC"] = item.ZDMC; //雨量站名称
                            graphic2.Attributes["YL24"] = item.YL24; //24小时雨量
                            graphic2.Attributes["DTNow"] = item.DTNow; //当前时间
                            graphic2.Attributes["Latitute"] = item.Latitute; //纬度
                            graphic2.Attributes["Longitute"] = item.Longitute; //经度
                            //将该Graphics添加到Graphicslayer中去
                            graphicslayer0.Graphics.Add(graphic2);
                            graphicslayer0.Opacity = 1;
                            if (currentValue > tip_Base.sideValue)
                            {
                                graphicslayer0.Visible = false;
                            }
                            else
                            {
                                graphicslayer0.Visible = true;
                            }


                            //鼠标移入事件
                            graphic2.MouseEnter += new MouseEventHandler(sh_graphic_MouseEnter);
                            graphic2.MouseLeave += new MouseEventHandler(sh_graphic_MouseLeave);
                            //鼠标点击事件


                            #region 站点名称


                            //动态添加文本
                            TextSymbol textSymbol = new TextSymbol()
                            {
                                FontFamily = new System.Windows.Media.FontFamily("Microsoft YaHei"),Symbol = textSymbol
                            };


                            graphicText1.Attributes["TextYLZMC"] = item.ZDMC;
                            graphicslayer11.Graphics.Add(graphicText1);
                            if (currentValue > tip_Base.sideValue)
                            {
                                graphicslayer11.Visible = false;
                            }
                            else
                            {
                                graphicslayer11.Visible = true;
                            }
                            #endregion


                            #region 水位/雨量 数值


                            if (item.YL24 != 0)
                            {
                                TextSymbol textSymbol2 = new TextSymbol()
                                {
                                    FontFamily = new System.Windows.Media.FontFamily("Microsoft YaHei"),Symbol = textSymbol2
                                };
                                graphicText2.Attributes["TextYL"] = item.YL24;
                                graphicslayer22.Graphics.Add(graphicText2);
                                if (currentValue > tip_Base.sideValue)
                                {
                                    graphicslayer22.Visible = false;
                                }
                                else
                                {
                                    graphicslayer22.Visible = true;
                                }
                            }


                            #endregion


                            #region 站点编码


                            //动态添加站点编码
                            TextSymbol textSymbol3 = new TextSymbol()
                            {
                                FontFamily = new System.Windows.Media.FontFamily("Microsoft YaHei"),Symbol = textSymbol3
                            };


                            graphicText3.Attributes["TextYLZBM"] = item.YLZBM;
                            graphicslayer33.Graphics.Add(graphicText3);
                            graphicslayer33.Visible = false;


                            #endregion


                            //左键点击事件
                            graphic2.MouseLeftButtonDown += new MouseButtonEventHandler(sh_graphic_MouseLeftButtonDown);
                            graphic2.MouseLeftButtonUp += new MouseButtonEventHandler(sh_graphic_MouseLeftButtonUp);


                            #endregion
                        }
                    }
                }
                else
                {
                    //不做任何处理,不添加任何点信息
                }
            }


4、在Map的ExtendChanged事件中处理

      

 private void myMap_ExtentChanged(object sender,ExtentEventArgs e)
{
      currentValue = (e.NewExtent.XMax - e.NewExtent.XMin) / (myMap.Layers.GetFullExtent().XMax - myMap.Layers.GetFullExtent().XMin);
      ShowSiteByGrade();
}

        /// <summary>
        /// 判断是在第一层还是第二层显示
        /// </summary>
        private void ShowSiteByGrade()
        {
            if (currentValue >= tip_Base.sideValue)
            {
                Graphicslayer graphicslayer0 = myMap.Layers["MyGraphicslayer3"] as Graphicslayer;
                graphicslayer0.Visible = false;
                Graphicslayer graphicslayer2 = myMap.Layers["Graphicslayer11"] as Graphicslayer;
                graphicslayer2.Visible = false;
                Graphicslayer graphicslayer3 = myMap.Layers["Graphicslayer22"] as Graphicslayer;
                graphicslayer3.Visible = false;
                Graphicslayer graphicslayer4 = myMap.Layers["Graphicslayer33"] as Graphicslayer;
                graphicslayer4.Visible = false;
            }
            else
            {
                Graphicslayer graphicslayer = myMap.Layers["MyGraphicslayer3"] as Graphicslayer;
                graphicslayer.Visible = true;
                Graphicslayer graphicslayer2 = myMap.Layers["Graphicslayer11"] as Graphicslayer;
                graphicslayer2.Visible = true;
                Graphicslayer graphicslayer3 = myMap.Layers["Graphicslayer22"] as Graphicslayer;
                graphicslayer3.Visible = true;
                Graphicslayer graphicslayer4 = myMap.Layers["Graphicslayer33"] as Graphicslayer;
                graphicslayer4.Visible = false;
            }
        }
     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐