爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 4066|回复: 2

MeteoInfo Java版二次开发教程十

[复制链接]

新浪微博达人勋

发表于 2013-11-6 10:07:16 | 显示全部楼层 |阅读模式

登录后查看更多精彩内容~

您需要 登录 才可以下载或查看,没有帐号?立即注册 新浪微博登陆

x
本帖最后由 ycjtj1dx202ly 于 2013-11-6 10:05 编辑

此次教程实现在状态栏中显示MapView和MapLayout中鼠标所在位置的地理坐标。
先在状态栏中加入分隔符和标签,标签更改变量为jLabel_Coordinate,用来显示鼠标所在位置的地理坐标。
然后增加mapView_MouseMoved和Layout_MouseMoved两个鼠标事件函数用来获取鼠标所在位置的地理坐标。如果是经纬度投影,可直接用screenToProj获得鼠标所在位置的经纬度;如果是其它投影,screenToProj获得的是该投影下的坐标位置(以米为单位),想要得到经纬度需要进行投影变换,定义投影前后的ProjectionInfo(fromProj即当前投影,toProj设为经纬度投影),用Reproject.reprojectPoints方法可将当前投影中鼠标所在点的坐标投影为经纬度坐标,具体代码如下:   
  1. private void mapView_MouseMoved(MouseEvent e) {
  2.         double pXY[] = this.layersLegend1.getActiveMapFrame().getMapView().screenToProj((double) e.getX(), (double) e.getY());
  3.         double projX = pXY[0];
  4.         double projY = pXY[1];
  5.         if (layersLegend1.getActiveMapFrame().getMapView().getProjection().isLonLatMap()) {
  6.             this.jLabel_Coordinate.setText("Lon: " + String.format("%1$.2f", projX) + "; Lat: " + String.format("%1$.2f", projY));
  7.         } else {
  8.             this.jLabel_Coordinate.setText("X: " + String.format("%1$.1f", projX) + "; Y: " + String.format("%1$.1f", projY));
  9.             String theText = this.jLabel_Coordinate.getText();
  10.             if (layersLegend1.getActiveMapFrame().getMapView().getProjection().getProjInfo().getProjectionName() == ProjectionNames.Robinson) {
  11.                 return;
  12.             }

  13.             ProjectionInfo toProj = KnownCoordinateSystems.geographic.world.WGS1984;
  14.             ProjectionInfo fromProj = layersLegend1.getActiveMapFrame().getMapView().getProjection().getProjInfo();
  15.             double[][] points = new double[1][];
  16.             points[0] = new double[]{projX, projY};
  17.             //double[] Z = new double[1];
  18.             try {
  19.                 Reproject.reprojectPoints(points, fromProj, toProj, 0, 1);
  20.                 this.jLabel_Coordinate.setText(theText + " (Lon: " + String.format("%1$.2f", points[0][0]) + "; Lat: "
  21.                         + String.format("%1$.2f", points[0][1]) + ")");
  22.             } catch (Exception ex) {
  23.                 //this.TSSL_Coord.Text = "X: " + ProjX.ToString("0.0") + "; Y: " + ProjY.ToString("0.0");
  24.             }
  25.         }
  26.     }
  27.     private void layout_MouseMoved(MouseEvent e) {
  28.         Point pageP = layersLegend1.getMapLayout().screenToPage(e.getX(), e.getY());
  29.         for (MapFrame mf : layersLegend1.getMapFrames()) {
  30.             Rectangle rect = mf.getLayoutBounds();
  31.             if (MIMath.pointInRectangle(pageP, rect)) {
  32.                 double pXY[] = mf.getMapView().screenToProj((double) (pageP.x - rect.x), (double) (pageP.y - rect.y), layersLegend1.getMapLayout().getZoom());
  33.                 double projX = pXY[0];
  34.                 double projY = pXY[1];
  35.                 if (mf.getMapView().getProjection().isLonLatMap()) {
  36.                     this.jLabel_Coordinate.setText("Lon: " + String.format("%1$.2f", projX) + "; Lat: " + String.format("%1$.2f", projY));
  37.                 } else {
  38.                     this.jLabel_Coordinate.setText("X: " + String.format("%1$.1f", projX) + "; Y: " + String.format("%1$.1f", projY));
  39.                     String theText = this.jLabel_Coordinate.getText();
  40.                     if (mf.getMapView().getProjection().getProjInfo().getProjectionName() == ProjectionNames.Robinson) {
  41.                         return;
  42.                     }

  43.                     ProjectionInfo toProj = KnownCoordinateSystems.geographic.world.WGS1984;
  44.                     ProjectionInfo fromProj = mf.getMapView().getProjection().getProjInfo();
  45.                     double[][] points = new double[1][];
  46.                     points[0] = new double[]{projX, projY};
  47.                     try {
  48.                         Reproject.reprojectPoints(points, fromProj, toProj, 0, 1);
  49.                         this.jLabel_Coordinate.setText(theText + " (Lon: " + String.format("%1$.2f", points[0][0]) + "; Lat: "
  50.                             + String.format("%1$.2f", points[0][1]) + ")");
  51.                     } catch (Exception ex) {
  52.                     //this.TSSL_Coord.Text = "X: " + ProjX.ToString("0.0") + "; Y: " + ProjY.ToString("0.0");
  53.                     }
  54.                 }

  55.                 break;
  56.             }
  57.         }
  58.     }

在setMapView()中将mapView_MouseMoved(MouseEvent e)函数添加到MapView1中,在Frmain()构造函数中将layout_MouseMoved(MouseEvent e)函数添加到mapLayout1中。具体代码如下:
      
   
  1.     private void setMapView() {        
  2.         //Add map view
  3.         this.mapView1.setLockViewUpdate(true);
  4.         this.jPanel_Map.removeAll();
  5.         javax.swing.GroupLayout jPanel_MapLayout = new javax.swing.GroupLayout(jPanel_Map);
  6.         jPanel_Map.setLayout(jPanel_MapLayout);
  7.         jPanel_MapLayout.setHorizontalGroup(
  8.                 jPanel_MapLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(mapView1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
  9.         jPanel_MapLayout.setVerticalGroup(
  10.                 jPanel_MapLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(mapView1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
  11.         this.mapView1.setLockViewUpdate(false);
  12.         if (_currentTool != null) {
  13.             _currentTool.doClick();
  14.         }

  15.         //Add mapView_MouseMoved
  16.         mapView1.addMouseMotionListener(new MouseMotionAdapter() {
  17.         @Override
  18.             public void mouseMoved(MouseEvent e) {
  19.             mapView_MouseMoved(e);
  20.             }
  21.         });
  22.         this.mapView1.setFocusable(true);
  23.         this.mapView1.requestFocusInWindow();
  24.     }


        
  1.     public FrmMain() {        
  2.         initComponents();
  3.         this.layersLegend1.getActiveMapFrame().setMapView(this.mapView1);
  4.         this.layersLegend1.setMapLayout(this.mapLayout1);
  5.         this.layersLegend1.setIsLayoutView(false);

  6.         //Set width and heigth
  7.         this.setSize(1000, 625);

  8.         //Load layer
  9.         this.loadLayers();

  10.         //Set initial tool
  11.         this.jButton_Pan.doClick();

  12.         //AddMapFrame_ChinaSouthSea
  13.         this.addMapFrame_ChinaSouthSea();

  14.         //addTitle
  15.         this.AddTitle();

  16.         //setMapView
  17.         this.setMapView();

  18.         //add layout_MouseMoved
  19.         mapLayout1.addMouseMotionListener(new MouseMotionAdapter() {
  20.         @Override
  21.         public void mouseMoved(MouseEvent e) {
  22.             layout_MouseMoved(e);
  23.         }
  24.     });

  25.         this.layersLegend1.addActiveMapFrameChangedListener(new IActiveMapFrameChangedListener() {
  26.             @Override
  27.             public void activeMapFrameChangedEvent(ActiveMapFrameChangedEvent event) {
  28.                 mapView1 = layersLegend1.getActiveMapFrame().getMapView();
  29.                 setMapView();
  30.                 if (jTabbedPane1.getSelectedIndex() == 0) {
  31.                     mapView1.paintLayers();
  32.                 }
  33.             }
  34.         });
  35.     }

调试结果如下:
经纬度投影

1.png

Lambert投影

2.png

密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2014-6-20 16:49:59 | 显示全部楼层
本科生学习中
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2017-6-13 21:19:31 | 显示全部楼层
{:eb502:}
密码修改失败请联系微信:mofangbao
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

Copyright ©2011-2014 bbs.06climate.com All Rights Reserved.  Powered by Discuz! (京ICP-10201084)

本站信息均由会员发表,不代表气象家园立场,禁止在本站发表与国家法律相抵触言论

快速回复 返回顶部 返回列表