- 积分
- 229
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2013-10-17
- 最后登录
- 1970-1-1
|
登录后查看更多精彩内容~
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
本帖最后由 ycjtj1dx202ly 于 2013-11-6 10:05 编辑
此次教程实现在状态栏中显示MapView和MapLayout中鼠标所在位置的地理坐标。
先在状态栏中加入分隔符和标签,标签更改变量为jLabel_Coordinate,用来显示鼠标所在位置的地理坐标。
然后增加mapView_MouseMoved和Layout_MouseMoved两个鼠标事件函数用来获取鼠标所在位置的地理坐标。如果是经纬度投影,可直接用screenToProj获得鼠标所在位置的经纬度;如果是其它投影,screenToProj获得的是该投影下的坐标位置(以米为单位),想要得到经纬度需要进行投影变换,定义投影前后的ProjectionInfo(fromProj即当前投影,toProj设为经纬度投影),用Reproject.reprojectPoints方法可将当前投影中鼠标所在点的坐标投影为经纬度坐标,具体代码如下:
- private void mapView_MouseMoved(MouseEvent e) {
- double pXY[] = this.layersLegend1.getActiveMapFrame().getMapView().screenToProj((double) e.getX(), (double) e.getY());
- double projX = pXY[0];
- double projY = pXY[1];
- if (layersLegend1.getActiveMapFrame().getMapView().getProjection().isLonLatMap()) {
- this.jLabel_Coordinate.setText("Lon: " + String.format("%1$.2f", projX) + "; Lat: " + String.format("%1$.2f", projY));
- } else {
- this.jLabel_Coordinate.setText("X: " + String.format("%1$.1f", projX) + "; Y: " + String.format("%1$.1f", projY));
- String theText = this.jLabel_Coordinate.getText();
- if (layersLegend1.getActiveMapFrame().getMapView().getProjection().getProjInfo().getProjectionName() == ProjectionNames.Robinson) {
- return;
- }
- ProjectionInfo toProj = KnownCoordinateSystems.geographic.world.WGS1984;
- ProjectionInfo fromProj = layersLegend1.getActiveMapFrame().getMapView().getProjection().getProjInfo();
- 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.jLabel_Coordinate.setText(theText + " (Lon: " + String.format("%1$.2f", points[0][0]) + "; Lat: "
- + String.format("%1$.2f", points[0][1]) + ")");
- } catch (Exception ex) {
- //this.TSSL_Coord.Text = "X: " + ProjX.ToString("0.0") + "; Y: " + ProjY.ToString("0.0");
- }
- }
- }
- private void layout_MouseMoved(MouseEvent e) {
- Point pageP = layersLegend1.getMapLayout().screenToPage(e.getX(), e.getY());
- for (MapFrame mf : layersLegend1.getMapFrames()) {
- Rectangle rect = mf.getLayoutBounds();
- if (MIMath.pointInRectangle(pageP, rect)) {
- double pXY[] = mf.getMapView().screenToProj((double) (pageP.x - rect.x), (double) (pageP.y - rect.y), layersLegend1.getMapLayout().getZoom());
- double projX = pXY[0];
- double projY = pXY[1];
- if (mf.getMapView().getProjection().isLonLatMap()) {
- this.jLabel_Coordinate.setText("Lon: " + String.format("%1$.2f", projX) + "; Lat: " + String.format("%1$.2f", projY));
- } else {
- this.jLabel_Coordinate.setText("X: " + String.format("%1$.1f", projX) + "; Y: " + String.format("%1$.1f", projY));
- String theText = this.jLabel_Coordinate.getText();
- if (mf.getMapView().getProjection().getProjInfo().getProjectionName() == ProjectionNames.Robinson) {
- return;
- }
- ProjectionInfo toProj = KnownCoordinateSystems.geographic.world.WGS1984;
- ProjectionInfo fromProj = mf.getMapView().getProjection().getProjInfo();
- double[][] points = new double[1][];
- points[0] = new double[]{projX, projY};
- try {
- Reproject.reprojectPoints(points, fromProj, toProj, 0, 1);
- this.jLabel_Coordinate.setText(theText + " (Lon: " + String.format("%1$.2f", points[0][0]) + "; Lat: "
- + String.format("%1$.2f", points[0][1]) + ")");
- } catch (Exception ex) {
- //this.TSSL_Coord.Text = "X: " + ProjX.ToString("0.0") + "; Y: " + ProjY.ToString("0.0");
- }
- }
- break;
- }
- }
- }
在setMapView()中将mapView_MouseMoved(MouseEvent e)函数添加到MapView1中,在Frmain()构造函数中将layout_MouseMoved(MouseEvent e)函数添加到mapLayout1中。具体代码如下:
- private void setMapView() {
- //Add map view
- this.mapView1.setLockViewUpdate(true);
- this.jPanel_Map.removeAll();
- javax.swing.GroupLayout jPanel_MapLayout = new javax.swing.GroupLayout(jPanel_Map);
- jPanel_Map.setLayout(jPanel_MapLayout);
- jPanel_MapLayout.setHorizontalGroup(
- jPanel_MapLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(mapView1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
- jPanel_MapLayout.setVerticalGroup(
- jPanel_MapLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(mapView1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
- this.mapView1.setLockViewUpdate(false);
- if (_currentTool != null) {
- _currentTool.doClick();
- }
- //Add mapView_MouseMoved
- mapView1.addMouseMotionListener(new MouseMotionAdapter() {
- @Override
- public void mouseMoved(MouseEvent e) {
- mapView_MouseMoved(e);
- }
- });
- this.mapView1.setFocusable(true);
- this.mapView1.requestFocusInWindow();
- }
- public FrmMain() {
- initComponents();
- this.layersLegend1.getActiveMapFrame().setMapView(this.mapView1);
- this.layersLegend1.setMapLayout(this.mapLayout1);
- this.layersLegend1.setIsLayoutView(false);
- //Set width and heigth
- this.setSize(1000, 625);
- //Load layer
- this.loadLayers();
- //Set initial tool
- this.jButton_Pan.doClick();
- //AddMapFrame_ChinaSouthSea
- this.addMapFrame_ChinaSouthSea();
- //addTitle
- this.AddTitle();
- //setMapView
- this.setMapView();
- //add layout_MouseMoved
- mapLayout1.addMouseMotionListener(new MouseMotionAdapter() {
- @Override
- public void mouseMoved(MouseEvent e) {
- layout_MouseMoved(e);
- }
- });
- this.layersLegend1.addActiveMapFrameChangedListener(new IActiveMapFrameChangedListener() {
- @Override
- public void activeMapFrameChangedEvent(ActiveMapFrameChangedEvent event) {
- mapView1 = layersLegend1.getActiveMapFrame().getMapView();
- setMapView();
- if (jTabbedPane1.getSelectedIndex() == 0) {
- mapView1.paintLayers();
- }
- }
- });
- }
调试结果如下:
经纬度投影
Lambert投影
|
|