ArcGIS API for Silverlight 实现修改地图上的工程点位置 .

来源:互联网 发布:网络7层协议 编辑:程序博客网 时间:2024/05/21 11:23
[csharp] view plaincopyprint?01. #region 处理工程点点击编辑相关事件   02.  03.        public Graphic editgraphics = null; //待编辑的Graphics图层   04.        public Graphic oldgraphics = null; //原先Graphics图层   05.        public Symbol symbolold = null;  06.  07.        /// <summary>   08.        /// 在地图上点击编辑点处理事件   09.        /// </summary>   10.        /// <param name="sender"></param>   11.        /// <param name="e"></param>   12.        void myMap_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)  13.        {  14.            ESRI.ArcGIS.Client.Projection.WebMercator mercator = new ESRI.ArcGIS.Client.Projection.WebMercator();  15.            try  16.            {  17.                if (editgraphics != null)  18.                {  19.                    if (isedit)  20.                    {  21.                        System.Windows.Point screenPoint = e.GetPosition(myMap);  22.                        ESRI.ArcGIS.Client.Geometry.MapPoint mapPoint = myMap.ScreenToMap(screenPoint);  23.                        double x = Math.Round(mapPoint.X, 4);  24.                        double y = Math.Round(mapPoint.Y, 4);  25.                        MapPoint mp = new MapPoint(x, y);  26.                        editgraphics.Geometry = mp;  27.                    }  28.                    else  29.                    {  30.                        editgraphics = oldgraphics;  31.                    }  32.                }  33.                else  34.                {  35.  36.                }  37.            }  38.            catch (Exception)  39.            {  40.                return;  41.            }  42.        }  43.  44.        void graphic_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)  45.        {  46.            Graphic graphic = sender as Graphic;  47.            oldgraphics = graphic; //保存原先的Graphics图层   48.            isedit = true;  49.            //选工程点   50.            if (graphic != null)  51.            {  52.                //将上一个图元还原第一个图元   53.                if (symbolold != null)  54.                {  55.                    editgraphics.Symbol = symbolold;  56.                }  57.  58.                editgraphics = graphic;  59.                symbolold = editgraphics.Symbol;  60.                editgraphics.Symbol = ((SimpleMarkerSymbol)this.FindName("SimpleSymbol"));  61.  62.                ESRI.ArcGIS.Client.Geometry.MapPoint mp = (MapPoint)graphic.Geometry;  63.                ESRI.ArcGIS.Client.Projection.WebMercator mercator = new ESRI.ArcGIS.Client.Projection.WebMercator();  64.                mp = WKIDConvert.mercator2lonlat(ESRI.ArcGIS.Client.Geometry.Geometry.NormalizeCentralMeridian(mp) as ESRI.ArcGIS.Client.Geometry.MapPoint);  65.                //打开Tab进行编辑操作   66.                this.gridTab2.Width = new GridLength(278, GridUnitType.Pixel);  67.                tbTip1.Text = "<<";  68.                string title = graphic.Attributes["NAME"].ToString(); //工程名称   69.                this.tbProjectName.Text = title;  70.                this.tbLatitute.Text = Math.Round(mp.X, 4).ToString(); //经度   71.                this.tbLongitute.Text = Math.Round(mp.Y, 4).ToString(); //纬度   72.            }  73.        }  74.  75.        void graphic_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)  76.        {  77.            Graphic graphic = sender as Graphic;  78.  79.            editgraphics.Symbol = ((SimpleMarkerSymbol)this.FindName("SimpleSymbol"));  80.  81.            ESRI.ArcGIS.Client.Geometry.MapPoint mp = (MapPoint)graphic.Geometry;  82.            ESRI.ArcGIS.Client.Projection.WebMercator mercator = new ESRI.ArcGIS.Client.Projection.WebMercator();  83.            mp = WKIDConvert.mercator2lonlat(ESRI.ArcGIS.Client.Geometry.Geometry.NormalizeCentralMeridian(mp) as ESRI.ArcGIS.Client.Geometry.MapPoint);  84.            string title = graphic.Attributes["NAME"].ToString(); //工程名称   85.            this.tbProjectName.Text = title;  86.            this.tblEditName.Text = title;  87.            this.tbLatitute.Text = Math.Round(mp.X, 4).ToString(); //经度   88.            this.tbLongitute.Text = Math.Round(mp.Y, 4).ToString(); //纬度   89.        }  90.  91.        void graphic_MouseMove(object sender, MouseEventArgs e)  92.        {  93.            Graphic graphic = sender as Graphic;  94.            Grid grid = new Grid();  95.            grid.Background = new SolidColorBrush(Colors.Blue);  96.            TextBlock msg = new TextBlock();  97.            msg.Foreground = new SolidColorBrush(Colors.White);  98.            msg.FontSize = 13;  99.            msg.FontFamily = new FontFamily("Microsoft YaHei");  100.            msg.Text = graphic.Attributes["NAME"].ToString();  101.            grid.Children.Add(msg);  102.            graphic.MapTip = grid;  103.        }  104.  105.        RichTextBox rtb;  106.  107.        void graphic_MouseRightButtonUp(object sender, MouseButtonEventArgs e)  108.        {  109.            if (isedit)  110.            {  111.                //只有在选中点开始编辑后,才可以取消编辑   112.                RTBContextMenu menu = new RTBContextMenu(rtb, this);  113.                menu.Show(e.GetPosition(LayoutRoot));  114.            }  115.            else  116.            {  117.                //什么也不执行   118.            }  119.        }  120.  121.        void graphic_MouseRightButtonDown(object sender, MouseButtonEventArgs e)  122.        {  123.            e.Handled = true;  124.        }  125.  126.        private void btnModify_Click(object sender, System.Windows.RoutedEventArgs e)  127.        {  128.            try  129.            {  130.                if (string.IsNullOrEmpty(this.tbProjectName.Text))  131.                {  132.                    MessageBox.Show("请先选择一个工程点!");  133.                }  134.                else  135.                {  136.                    MapPoint mp = (MapPoint)editgraphics.Geometry;  137.                    getDataSoapClient client = new getDataSoapClient();  138.                    client.updagePositionCompleted += new EventHandler<AsyncCompletedEventArgs>(client_updagePositionCompleted);  139.                    MapPoint mapPoint = WKIDConvert.mercator2lonlat(ESRI.ArcGIS.Client.Geometry.Geometry.NormalizeCentralMeridian(mp) as ESRI.ArcGIS.Client.Geometry.MapPoint);  140.                    client.updagePositionAsync(this.tbProjectName.Text, Math.Round(mapPoint.X, 4).ToString(), Math.Round(mapPoint.Y, 4).ToString());  141.                }  142.            }  143.            catch (Exception)  144.            {  145.                MessageBox.Show("请先选择一个工程点!");  146.            }  147.  148.        }  149.  150.        void client_updagePositionCompleted(object sender, AsyncCompletedEventArgs e)  151.        {  152.            //重新加载数据,这里需要维持地图缩放的比例   153.            ESRI.ArcGIS.Client.Projection.WebMercator mercator = new ESRI.ArcGIS.Client.Projection.WebMercator();  154.            ESRI.ArcGIS.Client.Geometry.Envelope selectedFeatureExtent = mercator.ToGeographic(editgraphics.Geometry).Extent; //选中点的位置   155.            double expandPercentage = 10;  156.  157.            //加数值后,聚焦(这里需要注意,进行地理坐标和墨卡托坐标的转换)   158.            double widthExpand = (selectedFeatureExtent.Width + 5) * (expandPercentage / 100);  159.            double heightExpand = (selectedFeatureExtent.Height + 5) * (expandPercentage / 100);  160.            ESRI.ArcGIS.Client.Geometry.Envelope displayExtent = new Envelope(WKIDConvert.lonlat2mercator(new MapPoint(selectedFeatureExtent.XMin - (widthExpand / 2), selectedFeatureExtent.YMin - (heightExpand / 2))), WKIDConvert.lonlat2mercator(new MapPoint(selectedFeatureExtent.XMax + (widthExpand / 2), selectedFeatureExtent.YMax + (heightExpand / 2))))  161.            {  162.                SpatialReference = new SpatialReference(102100)  163.            };  164.  165.            MessageBox.Show("工程位置更新成功!");  166.            //重新加载地图   167.            GetGCInfoByType(tip_Base.CurrentValue);  168.            //重新置文本输入框为空   169.            this.tbProjectName.Text = "";  170.            this.tbLatitute.Text = "";  171.            this.tbLongitute.Text = "";  172.        }  173. 174.        #endregion   175.          176.using System;  177.using System.Net;  178.using System.Windows;  179.using System.Windows.Controls;  180.using System.Windows.Documents;  181.using System.Windows.Ink;  182.using System.Windows.Input;  183.using System.Windows.Media;  184.using System.Windows.Media.Animation;  185.using System.Windows.Shapes;  186.using System.Windows.Media.Imaging;  187.using System.Windows.Media.Effects;  188.using ESRI.ArcGIS.Client.FeatureService.Symbols;  189.using ESRI.ArcGIS.Client.Geometry;  190.  191.  192.namespace MapClient.CommonClass  193.{  194.    public class RTBContextMenu : ContextMenu  195.    {  196.        RichTextBox rtb;  197.        GCSiteM _gcSite;  198.  199.        public RTBContextMenu(RichTextBox rtb, GCSiteM gcSite)  200.        {  201.            this.rtb = rtb;  202.            _gcSite = gcSite;  203.        }  204.  205.  206.        //构造菜单按钮并返回一个FrameworkElement对象   207.        protected override FrameworkElement GetContent()  208.        {  209.            Border border = new Border() { BorderBrush = new SolidColorBrush(Color.FromArgb(255, 167, 171, 176)), BorderThickness = new Thickness(1), Background = new SolidColorBrush(Colors.White) };  210.            border.Effect = new DropShadowEffect() { BlurRadius = 3, Color = Color.FromArgb(255, 230, 227, 236) };  211.  212.  213.            //取消选中   214.            Button tjspButton = new Button() { Height = 22, Margin = new Thickness(0, 0, 0, 0), HorizontalAlignment = HorizontalAlignment.Stretch, VerticalAlignment = VerticalAlignment.Top, HorizontalContentAlignment = HorizontalAlignment.Left };  215.            tjspButton.Style = Application.Current.Resources["ContextMenuButton"] as Style;  216.            tjspButton.Click += new RoutedEventHandler(tjspButton_Click);  217.  218.            tjspButton.Content = "取消选中";  219.  220.            border.Child = tjspButton;  221.            return border;  222.        }  223.  224.  225.        void tjspButton_Click(object sender, RoutedEventArgs e)  226.        {  227.            //恢复原来的颜色   228.            _gcSite.editgraphics.Symbol = new SimpleMarkerSymbol()  229.            {  230.                Color = new SolidColorBrush(ColorRevert.ToColor("#FF0551A7")),  231.                Size = 10,  232.                Style = ESRI.ArcGIS.Client.FeatureService.Symbols.SimpleMarkerSymbol.SimpleMarkerStyle.Circle  233.            };  234.            _gcSite.isedit = false;  235.  236.  237.            //重新加载数据,这里需要维持地图缩放的比例   238.            ESRI.ArcGIS.Client.Projection.WebMercator mercator = new ESRI.ArcGIS.Client.Projection.WebMercator();  239.            ESRI.ArcGIS.Client.Geometry.Envelope selectedFeatureExtent = mercator.ToGeographic(_gcSite.oldgraphics.Geometry).Extent; //原先选中点的位置   240.            double expandPercentage = 10;  241.  242.  243.            //加数值后,聚焦(这里需要注意,进行地理坐标和墨卡托坐标的转换)   244.            double widthExpand = (selectedFeatureExtent.Width + 5) * (expandPercentage / 100);  245.            double heightExpand = (selectedFeatureExtent.Height + 5) * (expandPercentage / 100);  246.            ESRI.ArcGIS.Client.Geometry.Envelope displayExt http:// ent = new Envelope(WKIDConvert.lonlat2mercator(new MapPoint(selectedFeatureExtent.XMin - (widthExpand / 2), selectedFeatureExtent.YMin - (heightExpand / 2))), WKIDConvert.lonlat2mercator(new MapPoint(selectedFeatureExtent.XMax + (widthExpand / 2), selectedFeatureExtent.YMax + (heightExpand / 2))))   247.            {  248.                SpatialReference = new SpatialReference(102100)  249.            };  250.  251.  252.            //重新加载地图   253.            _gcSite.GetGCInfoByType(tip_Base.CurrentValue);  254.            //重新置文本输入框为空   255.            _gcSite.tbProjectName.Text = "";  256.            _gcSite.tbLatitute.Text = "";  257.            _gcSite.tbLongitute.Text = "";  258.            Close();  259.        }  260.    }  261.}  


 

以上使用到的右键菜单功能ContextMenu.cs类请参考:http://blog.csdn.net/taomanman/article/details/7333612

 

 

原文地址http://blog.csdn.net/taomanman/article/details/8602366