- 积分
- 55941
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2011-6-21
- 最后登录
- 1970-1-1
|
登录后查看更多精彩内容~
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
这一讲介绍一下如何在状态栏中显示MapView和MapLayout中鼠标所在位置的地理坐标。已在MeteoInfo网站上更新了Demo程序(http://www.meteothinker.com/Downloads.aspx),效果如下:
1、首先在状态栏中增加一个ToolStripStatusLabel控件,命名为TSSL_Coord用了显示鼠标所在地理坐标。
2、增加MapView_MouseMove和Layout_MouseMove两个鼠标事件函数来获取鼠标所在位置的地理坐标。如果是经纬度投影,直接用ScreenToProj就可以获得鼠标所在位置的经纬度;如果是其它投影,ScreenToProj获得的是该投影下的坐标位置(以米为单位),想要得到经纬度需要进行投影变换,定义投影前后的ProjectionInfo(fromProj即当前投影,toProj设为经纬度投影),用Reproject.ReprojectPoints方法可将当前投影中鼠标所在点的坐标投影为经纬度坐标,详见以下代码:
- private void MapView_MouseMove(object sender, MouseEventArgs e)
- {
- double ProjX, ProjY;
- ProjX = 0;
- ProjY = 0;
- layersLegend1.ActiveMapFrame.MapView.ScreenToProj(ref ProjX, ref ProjY, e.X, e.Y);
- if (layersLegend1.ActiveMapFrame.MapView.Projection.IsLonLatMap)
- {
- this.TSSL_Coord.Text = "Lon: " + ProjX.ToString("0.00") + "; Lat: " + ProjY.ToString("0.00");
- }
- else
- {
- string theText = this.TSSL_Coord.Text = "X: " + ProjX.ToString("0.0") + "; Y: " + ProjY.ToString("0.0");
- if (layersLegend1.ActiveMapFrame.MapView.Projection.ProjInfo.Transform.ProjectionName == ProjectionNames.Robinson)
- return;
- ProjectionInfo toProj = KnownCoordinateSystems.Geographic.World.WGS1984;
- ProjectionInfo fromProj = layersLegend1.ActiveMapFrame.MapView.Projection.ProjInfo;
- double[][] points = new double[1][];
- points[0] = new double[] { ProjX, ProjY };
- //double[] Z = new double[1];
- try
- {
- Reproject.ReprojectPoints(points, fromProj, toProj, 0, 1);
- this.TSSL_Coord.Text = theText + " (Lon: " + points[0][0].ToString("0.00") + "; Lat: " +
- points[0][1].ToString("0.00") + ")";
- }
- catch
- {
- //this.TSSL_Coord.Text = "X: " + ProjX.ToString("0.0") + "; Y: " + ProjY.ToString("0.0");
- }
- }
- }
复制代码- private void Layout_MouseMove(object sender, MouseEventArgs e)
- {
- Point pageP = layersLegend1.MapLayout.ScreenToPage(e.X, e.Y);
- foreach (MapFrame mf in layersLegend1.MapFrames)
- {
- Rectangle rect = mf.LayoutBounds;
- if (MIMath.PointInRectangle(pageP, rect))
- {
- double ProjX, ProjY;
- ProjX = 0;
- ProjY = 0;
- mf.MapView.ScreenToProj(ref ProjX, ref ProjY, pageP.X - rect.Left, pageP.Y - rect.Top, layersLegend1.MapLayout.Zoom);
- //ProjX = ProjX * MapDocument.MapLayout.Zoom;
- //ProjY = ProjY / MapDocument.MapLayout.Zoom;
- if (mf.MapView.Projection.IsLonLatMap)
- {
- this.TSSL_Coord.Text = "Lon: " + ProjX.ToString("0.00") + "; Lat: " + ProjY.ToString("0.00");
- }
- else
- {
- string theText = this.TSSL_Coord.Text = "X: " + ProjX.ToString("0.0") + "; Y: " + ProjY.ToString("0.0");
- if (mf.MapView.Projection.ProjInfo.Transform.ProjectionName == ProjectionNames.Robinson)
- return;
- ProjectionInfo toProj = KnownCoordinateSystems.Geographic.World.WGS1984;
- ProjectionInfo fromProj = mf.MapView.Projection.ProjInfo;
- double[][] points = new double[1][];
- points[0] = new double[] { ProjX, ProjY };
- //double[] Z = new double[1];
- try
- {
- Reproject.ReprojectPoints(points, fromProj, toProj, 0, 1);
- this.TSSL_Coord.Text = theText + " (Lon: " + points[0][0].ToString("0.00") + "; Lat: " +
- points[0][1].ToString("0.00") + ")";
- }
- catch
- {
- //this.TSSL_Coord.Text = "X: " + ProjX.ToString("0.0") + "; Y: " + ProjY.ToString("0.0");
- }
- }
- break;
- }
- }
- }
复制代码
3、在SetMapView()函数中将MapView_MouseMove事件函数增加到mapView1的MouseMove事件中,在frmMain_Load函数中将Layout_MouseMove事件函数增加到layersLegend的MapLayout的MouseMove事件中,代码如下:
- private void SetMapView()
- {
- //Add map view
- tabControl1.TabPages[0].Controls.Clear();
- tabControl1.TabPages[0].Controls.Add(mapView1);
- mapView1.Dock = DockStyle.Fill;
- mapView1.MouseMove += new MouseEventHandler(this.MapView_MouseMove);
- //mapView1.MouseDown += new MouseEventHandler(this.MapView_MouseDown);
- //mapView1.GraphicSeleted += new EventHandler(this.MapView_GraphicSelected);
- }
复制代码- private void frmMain_Load(object sender, EventArgs e)
- {
- //Set width and height
- this.Width = 1000;
- this.Height = 625;
- //Load layers
- LoadLayers();
- //Add title
- AddTitle();
- //Add South China Sea map frame
- AddMapFrame_ChinaSouthSea();
- //Set map view
- SetMapView();
- //Set map layout mouse move event
- layersLegend1.MapLayout.MouseMove += new MouseEventHandler(Layout_MouseMove);
- //Add ActiveMapFrameChanged event
- layersLegend1.ActiveMapFrameChanged += new EventHandler(ActiveMapFrameChanged);
- //Set initial tool
- TSB_Pan.PerformClick();
- }
复制代码
|
|