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

简介: 原文:ArcGIS API for Silverlight 实现修改地图上的工程点位置 #region 处理工程点点击编辑相关事件 public Graphic editgraphics = null; //待编辑的Graphics图层 public...
原文: ArcGIS API for Silverlight 实现修改地图上的工程点位置


 #region 处理工程点点击编辑相关事件

        public Graphic editgraphics = null; //待编辑的Graphics图层
        public Graphic oldgraphics = null; //原先Graphics图层
        public Symbol symbolold = null;

        /// <summary>
        /// 在地图上点击编辑点处理事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void myMap_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            ESRI.ArcGIS.Client.Projection.WebMercator mercator = new ESRI.ArcGIS.Client.Projection.WebMercator();
            try
            {
                if (editgraphics != null)
                {
                    if (isedit)
                    {
                        System.Windows.Point screenPoint = e.GetPosition(myMap);
                        ESRI.ArcGIS.Client.Geometry.MapPoint mapPoint = myMap.ScreenToMap(screenPoint);
                        double x = Math.Round(mapPoint.X, 4);
                        double y = Math.Round(mapPoint.Y, 4);
                        MapPoint mp = new MapPoint(x, y);
                        editgraphics.Geometry = mp;
                    }
                    else
                    {
                        editgraphics = oldgraphics;
                    }
                }
                else
                {

                }
            }
            catch (Exception)
            {
                return;
            }
        }

        void graphic_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Graphic graphic = sender as Graphic;
            oldgraphics = graphic; //保存原先的Graphics图层
            isedit = true;
            //选工程点
            if (graphic != null)
            {
                //将上一个图元还原第一个图元
                if (symbolold != null)
                {
                    editgraphics.Symbol = symbolold;
                }

                editgraphics = graphic;
                symbolold = editgraphics.Symbol;
                editgraphics.Symbol = ((SimpleMarkerSymbol)this.FindName("SimpleSymbol"));

                ESRI.ArcGIS.Client.Geometry.MapPoint mp = (MapPoint)graphic.Geometry;
                ESRI.ArcGIS.Client.Projection.WebMercator mercator = new ESRI.ArcGIS.Client.Projection.WebMercator();
                mp = WKIDConvert.mercator2lonlat(ESRI.ArcGIS.Client.Geometry.Geometry.NormalizeCentralMeridian(mp) as ESRI.ArcGIS.Client.Geometry.MapPoint);
                //打开Tab进行编辑操作
                this.gridTab2.Width = new GridLength(278, GridUnitType.Pixel);
                tbTip1.Text = "<<";
                string title = graphic.Attributes["NAME"].ToString(); //工程名称
                this.tbProjectName.Text = title;
                this.tbLatitute.Text = Math.Round(mp.X, 4).ToString(); //经度
                this.tbLongitute.Text = Math.Round(mp.Y, 4).ToString(); //纬度
            }
        }

        void graphic_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Graphic graphic = sender as Graphic;

            editgraphics.Symbol = ((SimpleMarkerSymbol)this.FindName("SimpleSymbol"));

            ESRI.ArcGIS.Client.Geometry.MapPoint mp = (MapPoint)graphic.Geometry;
            ESRI.ArcGIS.Client.Projection.WebMercator mercator = new ESRI.ArcGIS.Client.Projection.WebMercator();
            mp = WKIDConvert.mercator2lonlat(ESRI.ArcGIS.Client.Geometry.Geometry.NormalizeCentralMeridian(mp) as ESRI.ArcGIS.Client.Geometry.MapPoint);
            string title = graphic.Attributes["NAME"].ToString(); //工程名称
            this.tbProjectName.Text = title;
            this.tblEditName.Text = title;
            this.tbLatitute.Text = Math.Round(mp.X, 4).ToString(); //经度
            this.tbLongitute.Text = Math.Round(mp.Y, 4).ToString(); //纬度
        }

        void graphic_MouseMove(object sender, MouseEventArgs e)
        {
            Graphic graphic = sender as Graphic;
            Grid grid = new Grid();
            grid.Background = new SolidColorBrush(Colors.Blue);
            TextBlock msg = new TextBlock();
            msg.Foreground = new SolidColorBrush(Colors.White);
            msg.FontSize = 13;
            msg.FontFamily = new FontFamily("Microsoft YaHei");
            msg.Text = graphic.Attributes["NAME"].ToString();
            grid.Children.Add(msg);
            graphic.MapTip = grid;
        }

        RichTextBox rtb;

        void graphic_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (isedit)
            {
                //只有在选中点开始编辑后,才可以取消编辑
                RTBContextMenu menu = new RTBContextMenu(rtb, this);
                menu.Show(e.GetPosition(LayoutRoot));
            }
            else
            {
                //什么也不执行
            }
        }

        void graphic_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
        }

        private void btnModify_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(this.tbProjectName.Text))
                {
                    MessageBox.Show("请先选择一个工程点!");
                }
                else
                {
                    MapPoint mp = (MapPoint)editgraphics.Geometry;
                    getDataSoapClient client = new getDataSoapClient();
                    client.updagePositionCompleted += new EventHandler<AsyncCompletedEventArgs>(client_updagePositionCompleted);
                    MapPoint mapPoint = WKIDConvert.mercator2lonlat(ESRI.ArcGIS.Client.Geometry.Geometry.NormalizeCentralMeridian(mp) as ESRI.ArcGIS.Client.Geometry.MapPoint);
                    client.updagePositionAsync(this.tbProjectName.Text, Math.Round(mapPoint.X, 4).ToString(), Math.Round(mapPoint.Y, 4).ToString());
                }
            }
            catch (Exception)
            {
                MessageBox.Show("请先选择一个工程点!");
            }

        }

        void client_updagePositionCompleted(object sender, AsyncCompletedEventArgs e)
        {
            //重新加载数据,这里需要维持地图缩放的比例
            ESRI.ArcGIS.Client.Projection.WebMercator mercator = new ESRI.ArcGIS.Client.Projection.WebMercator();
            ESRI.ArcGIS.Client.Geometry.Envelope selectedFeatureExtent = mercator.ToGeographic(editgraphics.Geometry).Extent; //选中点的位置
            double expandPercentage = 10;

            //加数值后,聚焦(这里需要注意,进行地理坐标和墨卡托坐标的转换)
            double widthExpand = (selectedFeatureExtent.Width + 5) * (expandPercentage / 100);
            double heightExpand = (selectedFeatureExtent.Height + 5) * (expandPercentage / 100);
            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))))
            {
                SpatialReference = new SpatialReference(102100)
            };

            MessageBox.Show("工程位置更新成功!");
            //重新加载地图
            GetGCInfoByType(tip_Base.CurrentValue);
            //重新置文本输入框为空
            this.tbProjectName.Text = "";
            this.tbLatitute.Text = "";
            this.tbLongitute.Text = "";
        }

        #endregion
        
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using System.Windows.Media.Effects;
using ESRI.ArcGIS.Client.FeatureService.Symbols;
using ESRI.ArcGIS.Client.Geometry;


namespace MapClient.CommonClass
{
    public class RTBContextMenu : ContextMenu
    {
        RichTextBox rtb;
        GCSiteM _gcSite;

        public RTBContextMenu(RichTextBox rtb, GCSiteM gcSite)
        {
            this.rtb = rtb;
            _gcSite = gcSite;
        }


        //构造菜单按钮并返回一个FrameworkElement对象
        protected override FrameworkElement GetContent()
        {
            Border border = new Border() { BorderBrush = new SolidColorBrush(Color.FromArgb(255, 167, 171, 176)), BorderThickness = new Thickness(1), Background = new SolidColorBrush(Colors.White) };
            border.Effect = new DropShadowEffect() { BlurRadius = 3, Color = Color.FromArgb(255, 230, 227, 236) };


            //取消选中
            Button tjspButton = new Button() { Height = 22, Margin = new Thickness(0, 0, 0, 0), HorizontalAlignment = HorizontalAlignment.Stretch, VerticalAlignment = VerticalAlignment.Top, HorizontalContentAlignment = HorizontalAlignment.Left };
            tjspButton.Style = Application.Current.Resources["ContextMenuButton"] as Style;
            tjspButton.Click += new RoutedEventHandler(tjspButton_Click);

            tjspButton.Content = "取消选中";

            border.Child = tjspButton;
            return border;
        }


        void tjspButton_Click(object sender, RoutedEventArgs e)
        {
            //恢复原来的颜色
            _gcSite.editgraphics.Symbol = new SimpleMarkerSymbol()
            {
                Color = new SolidColorBrush(ColorRevert.ToColor("#FF0551A7")),
                Size = 10,
                Style = ESRI.ArcGIS.Client.FeatureService.Symbols.SimpleMarkerSymbol.SimpleMarkerStyle.Circle
            };
            _gcSite.isedit = false;


            //重新加载数据,这里需要维持地图缩放的比例
            ESRI.ArcGIS.Client.Projection.WebMercator mercator = new ESRI.ArcGIS.Client.Projection.WebMercator();
            ESRI.ArcGIS.Client.Geometry.Envelope selectedFeatureExtent = mercator.ToGeographic(_gcSite.oldgraphics.Geometry).Extent; //原先选中点的位置
            double expandPercentage = 10;


            //加数值后,聚焦(这里需要注意,进行地理坐标和墨卡托坐标的转换)
            double widthExpand = (selectedFeatureExtent.Width + 5) * (expandPercentage / 100);
            double heightExpand = (selectedFeatureExtent.Height + 5) * (expandPercentage / 100);
            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))))
            {
                SpatialReference = new SpatialReference(102100)
            };


            //重新加载地图
            _gcSite.GetGCInfoByType(tip_Base.CurrentValue);
            //重新置文本输入框为空
            _gcSite.tbProjectName.Text = "";
            _gcSite.tbLatitute.Text = "";
            _gcSite.tbLongitute.Text = "";
            Close();
        }
    }
}


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



目录
相关文章
|
1月前
|
缓存 API 定位技术
使用Python调用百度地图API实现地址查询
使用Python调用百度地图API实现地址查询
100 0
|
8月前
|
定位技术 API 开发工具
Android 按照步骤接入百度地图API,定位显示不了解决办法
Android 按照步骤接入百度地图API,定位显示不了解决办法
227 0
|
9月前
|
定位技术 API
百度地图API开发:多边形覆盖物鼠标高亮效果及单击事件
百度地图API开发:多边形覆盖物鼠标高亮效果及单击事件
163 0
|
9月前
|
数据可视化 搜索推荐 JavaScript
数据可视化大屏百度地图区域掩膜MapMask实现地图指定区域非省市县行政区显示的实战案例解析(JavaScript API GL、个性化地图定制、指定区域经纬度拾取转化)
数据可视化大屏百度地图区域掩膜MapMask实现地图指定区域非省市县行政区显示的实战案例解析(JavaScript API GL、个性化地图定制、指定区域经纬度拾取转化)
689 0
|
3月前
|
JSON JavaScript 定位技术
Vue中使用echarts@4.x中国地图及AMap相关API的使用
Vue中使用echarts@4.x中国地图及AMap相关API的使用
156 0
Vue中使用echarts@4.x中国地图及AMap相关API的使用
|
4月前
|
SQL 存储 DataWorks
DataWorks中API、desc、数据地图,这3个怎么对起来?
DataWorks中API、desc、数据地图,这3个怎么对起来?
40 0
|
9月前
|
移动开发 JavaScript 前端开发
数据可视化大屏百度地图手机端标注开发实战案例解析(jsAPI接口、标注分类图片、文本标签、分类筛选、自适应高度信息弹窗、PHP后端API)
数据可视化大屏百度地图手机端标注开发实战案例解析(jsAPI接口、标注分类图片、文本标签、分类筛选、自适应高度信息弹窗、PHP后端API)
147 0
|
9月前
|
定位技术 API 容器
百度地图API开发:停车场分布标注和检索静态版
百度地图API开发:停车场分布标注和检索静态版
92 0
|
9月前
|
移动开发 JavaScript 定位技术
百度地图开发:地图调起API(Web端)使用终点经纬度直接调用百度地图导航信息的解决方案
百度地图开发:地图调起API(Web端)使用终点经纬度直接调用百度地图导航信息的解决方案
222 0
|
4月前
|
定位技术 API
百度地图API的使用教程以及案例(二)
百度地图API的使用教程以及案例