How to create a map control

Steps

The following steps will allow you to embed a map control into your Java application and display a layer that you can navigate with.

  1. Add the ArcGIS Runtime dependent jar files to the build path of your Java project. You will locate these files in the <ArcGIS Runtime SDK installation directory>/sdk/jars folder laid down at installation.
  2. Import the Swing map control called JMap found in the com.esri.map package.
    import com.esri.map.JMap;
    
  3. Add the map control (JMap) to the ContentPane of the JFrame. Set the frame's window listener to ensure that the map is disposed of correctly when the window is closed.
    private JFrame theFrame;
    private JMap map;
    ...
    theFrame = new JFrame();
    theFrame.setBounds(100, 100, 450, 300);
    theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    theFrame.getContentPane().setLayout(new BorderLayout(0, 0));
    
    // dispose map just before application window is closed.
    theFrame.addWindowListener(new WindowAdapter() {
     @Override
     public void windowClosing(WindowEvent windowEvent) {
      super.windowClosing(windowEvent);
      map.dispose();
     }
    });
    
    map = new JMap();
    theFrame.getContentPane().add(map);
    
  4. Import the tiled map service class found in the com.esri.map package.
    import com.esri.map.ArcGISTiledMapServiceLayer;
    
  5. Create the ArcGISTiledMapServiceLayer and provide the URL to the map service endpoint. Add the layer to the map's list of layers.
    ArcGISTiledMapServiceLayer tiledLayer = new ArcGISTiledMapServiceLayer(
     "http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer");
    map.getLayers().add(tiledLayer);
    
  6. When the layer has been added and the map is ready, set the initial map extent. Create a map listener and override the mapReady event. When the mapReady event is fired, call the setExtent method on the map.
    map.addMapEventListener(new MapEventListenerAdapter() { 
     @Override
     public void mapReady(MapEvent arg0) { 
      SwingUtilities.invokeLater(new Runnable() { 
        @Override 
        public void run() { 
         map.setExtent(new Envelope(-14384172, 4578761, -11982215, 6180881)); 
        }
      });
     }
    }
    
  7. Compile and run your application.
  8. Navigate around the map by zooming, panning and using the mouse wheel.

Alternatively, follow the steps here which describe how to use the Eclipse New Project wizard called "ArcGIS Runtime Java Map Application" to add a new project to Eclipse. All of the above steps will be completed for you.

2/7/2013