Map coordinates

Download Sample Viewer

Description

This application shows how to use the CoordinateConversion class to convert a point geometry into a coordinate string or a map coordinate when the mouse moves over the map. As you move the mouse around on the map, the various coordinates will display in the application.

Code snippet


    private class MouseMoveOverlay extends MapOverlay {
      @Override
      public void onMouseMoved(MouseEvent event) {
        try {
          java.awt.Point screenPoint = event.getPoint();
          com.esri.core.geometry.Point mapPoint = map.toMapPoint(screenPoint.x, screenPoint.y);
          String screenCoords = "Screen Coordinates: X = " + screenPoint.x + ", Y = " + screenPoint.y;
          String mapCoords = "Map Coordinates: X = " + decimalFormat.format(mapPoint.getX()) 
          + ", Y = " + decimalFormat.format(mapPoint.getY());
          jTextArea.setText(screenCoords + "\n" + mapCoords);
        }
      }
    }
  

Sample Code

/* Copyright 2014 Esri

All rights reserved under the copyright laws of the United States
and applicable international laws, treaties, and conventions.

You may freely redistribute and use this sample code, with or
without modification, provided you include the original copyright
notice and use restrictions.

See the use restrictions.*/
package com.esri.client.samples.map;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.DecimalFormat;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

import com.esri.core.geometry.CoordinateConversion;
import com.esri.core.geometry.CoordinateConversion.UTMConversionMode;
import com.esri.map.ArcGISTiledMapServiceLayer;
import com.esri.map.JMap;
import com.esri.map.MapOverlay;

/***
 * This application shows how to use the {@link CoordinateConversion} 
 * class to convert a point geometry into a coordinate string or a map
 * coordinate when the mouse moves over the map.
 */
public class ShowMouseCoordinates {

  private JMap map;
  private JTextArea jTextArea;
  private static DecimalFormat decimalFormat = new DecimalFormat("##.###");
  
  // Default constructor
  public ShowMouseCoordinates() { }
  
  // ------------------------------------------------------------------------
  // Core functionality
  // ------------------------------------------------------------------------
  
  private class MouseMoveOverlay extends MapOverlay {
    private static final long serialVersionUID = 1L;

    @Override
    public void onMouseMoved(MouseEvent arg0) {
      try {
        if (!map.isReady()) {
          return;
        }

        java.awt.Point screenPoint = arg0.getPoint();
        com.esri.core.geometry.Point mapPoint = map.toMapPoint(screenPoint.x, screenPoint.y);

        String screenCoords = "Screen Coordinates: X = " + screenPoint.x
            + ", Y = " + screenPoint.y;
        String mapCoords = "Map Coordinates: X = " + decimalFormat.format(mapPoint.getX())
            + ", Y = " + decimalFormat.format(mapPoint.getY());
        String decimalDegrees = "Decimal Degrees: " 
            + CoordinateConversion.pointToDecimalDegrees(mapPoint, map.getSpatialReference(), 2);
        String degreesDecimalMinutes = "Degrees Decimal Minutes: " 
            + CoordinateConversion.pointToDegreesDecimalMinutes(mapPoint, map.getSpatialReference(), 2);
        String degreesMinutesSeconds = "Degrees Minutes Seconds: " 
            + CoordinateConversion.pointToDegreesMinutesSeconds(mapPoint, map.getSpatialReference(), 2);
        String GARSCoordinates = "GARS Coordinates: " 
            + CoordinateConversion.pointToGars(mapPoint, map.getSpatialReference());
        String GEOREFCoordinates = "Georef Coordinates: " 
            + CoordinateConversion.pointToGeoref(mapPoint, map.getSpatialReference(), 2, true);
        String USNGCoordinates = "USNG Coordinates: " 
            + CoordinateConversion.pointToUsng(mapPoint, map.getSpatialReference(), 2, true, true);
        String UTMCoordinates = "UTM Coordinates: ";
        String UTMCoordinatesValue = "";
        // for UTM-conversion, y-value should be in range of (-80, 84) in SR 4326
        if (mapPoint.getY() >= 84 || mapPoint.getY() <= -80) {
          UTMCoordinatesValue = "Out-of-range";
        } else {
          UTMCoordinatesValue = CoordinateConversion.pointToUtm(
            mapPoint, map.getSpatialReference(), UTMConversionMode.DEFAULT, true);
        }
        
        jTextArea.setText(
          screenCoords + "\n" + 
          mapCoords + "\n" + 
          decimalDegrees + "\n" + 
          degreesDecimalMinutes + "\n" + 
          degreesMinutesSeconds + "\n" + 
          GARSCoordinates + "\n" + 
          GEOREFCoordinates + "\n" + 
          USNGCoordinates + "\n" + 
          UTMCoordinates + UTMCoordinatesValue);

      } finally {
        super.onMouseMoved(arg0);
      }
    }
  }

  // ------------------------------------------------------------------------
  // Static methods
  // ------------------------------------------------------------------------
  /**
   * Starting point of this application.
   * @param args arguments to this application.
   */
  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
        try {
          // instance of this application
          ShowMouseCoordinates showMouseCoordinatesApp = new ShowMouseCoordinates();

          // create the UI, including the map, for the application.
          JFrame appWindow = showMouseCoordinatesApp.createWindow();
          appWindow.setContentPane(showMouseCoordinatesApp.createUI());
          appWindow.setVisible(true);
        } catch (Exception e) {
          // on any error, display the stack trace.
          e.printStackTrace();
        }
      }
    });
  }

  // ------------------------------------------------------------------------
  // Public methods
  // ------------------------------------------------------------------------

  public JComponent createUI() {

    JComponent contentPane = createContentPane();

    map = createMap();

    contentPane.add(map, BorderLayout.CENTER);
    contentPane.add(getJTextArea(), BorderLayout.SOUTH);

    return contentPane;
  }

  // ------------------------------------------------------------------------
  // Private methods
  // ------------------------------------------------------------------------

  private JMap createMap() {

    final JMap jMap = new JMap();

    jMap.addMapOverlay(new MouseMoveOverlay());

    // add tiled map service layer
    ArcGISTiledMapServiceLayer worldLayer = new ArcGISTiledMapServiceLayer(
      "http://services.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer");
    jMap.getLayers().add(worldLayer);

    return jMap;
  }

  /**
   * Creates a content pane.
   * @return a content pane.
   */
  private static JLayeredPane createContentPane() {
    JLayeredPane contentPane = new JLayeredPane();
    contentPane.setBounds(100, 100, 1000, 700);
    contentPane.setLayout(new BorderLayout(0, 0));
    contentPane.setVisible(true);
    return contentPane;
  }

  private JTextArea getJTextArea() {
    if (jTextArea == null) {
      jTextArea = new JTextArea(9,1);
      jTextArea.setFont(new Font(jTextArea.getFont().getName(), Font.PLAIN, 12));
      jTextArea.setBorder(BorderFactory.createEmptyBorder(5,10,5,10));
      jTextArea.setEditable(false);
      jTextArea.setText("Map coordinates will display here as you move the mouse cursor around the map.");
    }
    return jTextArea;
  }
  
  /**
   * Creates the application window.
   * @return a window.
   */
  private JFrame createWindow() {
    JFrame window = new JFrame("Map Coordinates Application");
    window.setBounds(100, 100, 1000, 700);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.getContentPane().setLayout(new BorderLayout(0, 0));
    window.addWindowListener(new WindowAdapter() {
      @Override
      public void windowClosing(WindowEvent windowEvent) {
        super.windowClosing(windowEvent);
        map.dispose();
      }
    });
    return window;
  }
}
Feedback on this topic?