Map tips

Download Sample Viewer

Description

This application shows how to use the MapTip class to display map tips when the mouse idles over a feature in a FeatureLayer or a graphic in a GraphicsLayer. In this application the points feature layer displays map tips with longitude, latitude, and a formatted date field, while the rivers feature layer displays a name a system field. The points layer MapTip can be customized dynamically via an options panel in the application.

Code snippet


  // create the map of the fields we want the map tips to display
  LinkedHashMap<String, String> displayFields = new LinkedHashMap<String, String>();
  displayFields.put("latitude", "Latitude: ");
  displayFields.put("longitude", "Longitude: ");
 
  // create the MapTip and set it on the layer
  mapTip = new MapTip(displayFields);
  pointsLayer.setMapTip(mapTip);
  

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.displayinfo;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.LinkedHashMap;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;

import com.esri.core.geometry.Envelope;
import com.esri.core.renderer.SimpleRenderer;
import com.esri.core.symbol.SimpleLineSymbol;
import com.esri.core.symbol.SimpleMarkerSymbol;
import com.esri.core.symbol.SimpleMarkerSymbol.Style;
import com.esri.map.ArcGISFeatureLayer;
import com.esri.map.ArcGISTiledMapServiceLayer;
import com.esri.map.JMap;
import com.esri.map.LayerInitializeCompleteEvent;
import com.esri.map.LayerInitializeCompleteListener;
import com.esri.map.LayerList;
import com.esri.map.MapTip;

/***
 * This application shows how to use the MapTip class to display map tips
 * when the mouse idles over a feature in an ArcGISFeatureLayer or a graphic in
 * a GraphicsLayer. In this application the points feature layer displays
 * map tips with longitude, latitude, and a formatted date field, while the
 * rivers feature layer displays a name and a system field.  The points layer
 * MapTip can be customized dynamically via an options panel in the
 * application.
 */
public class MapTipsApp {

  // symbology
  private final static SimpleMarkerSymbol SYM_POINT  = new SimpleMarkerSymbol(new Color(200, 0, 0, 200), 14, Style.CIRCLE);
  private final static SimpleLineSymbol SYM_RIVER = new SimpleLineSymbol(new Color(0, 0, 200), 1);

  // resources
  private final String URL_TOPO =
      "http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer";
  private final String URL_RIVERS =
      "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/1";
  private final String URL_POINTS =
      "http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Earthquakes/EarthquakesFromLastSevenDays/FeatureServer/0";

  // JMap
  private JMap map;
  private MapTip mapTip;

  //------------------------------------------------------------------------
  // Constructors
  //------------------------------------------------------------------------
  public MapTipsApp() {
  }

  //------------------------------------------------------------------------
  // Core functionality
  //------------------------------------------------------------------------
  /**
   * Creates a map with a base layer and two feature layers.
   * Each feature layers has a MapTip with custom display fields.
   * @return a map.
   */
  private JMap createMap() throws Exception {

    final JMap jMap = new JMap();

    jMap.setExtent(new Envelope(-14384172, 4578761, -11982215, 6180881));

    // tiled map service layer
    ArcGISTiledMapServiceLayer baseLayer = new ArcGISTiledMapServiceLayer(URL_TOPO);
    final LayerList layers = jMap.getLayers();
    layers.add(baseLayer);

    // feature layer for points
    final ArcGISFeatureLayer pointsLayer = new ArcGISFeatureLayer(URL_POINTS);
    layers.add(pointsLayer);
    pointsLayer.setRenderer(new SimpleRenderer(SYM_POINT));
    pointsLayer.addLayerInitializeCompleteListener(new LayerInitializeCompleteListener() {
      @Override
      public void layerInitializeComplete(LayerInitializeCompleteEvent e) {
        // create the map of the fields we want the map tips to display
        LinkedHashMap<String, String> displayFields = new LinkedHashMap<>();
        displayFields.put("latitude", "Latitude: ");
        displayFields.put("longitude", "Longitude: ");
        displayFields.put("datetime", "Time: ");
        // create the MapTip and set it on the layer
        mapTip = new MapTip(displayFields);
        pointsLayer.setMapTip(mapTip);
      }
    });

    // feature layer for rivers
    final ArcGISFeatureLayer riversLayer = new ArcGISFeatureLayer(URL_RIVERS);
    riversLayer.setRenderer(new SimpleRenderer(SYM_RIVER));
    layers.add(riversLayer);
    riversLayer.addLayerInitializeCompleteListener(new LayerInitializeCompleteListener() {
      @Override
      public void layerInitializeComplete(LayerInitializeCompleteEvent e) {
        // create the map of the fields we want the map tips to display
        LinkedHashMap<String, String> displayFields = new LinkedHashMap<>();
        displayFields.put("NAME", "Name: ");
        displayFields.put("SYSTEM", "System: ");
        // create the MapTip and set it on the layer
        riversLayer.setMapTip(new MapTip(displayFields));
      }
    });

    return jMap;
  }

  //------------------------------------------------------------------------
  // 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
          MapTipsApp app = new MapTipsApp();

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

  //------------------------------------------------------------------------
  // Public methods
  //------------------------------------------------------------------------
  /**
   * Creates and displays the UI, including the map, for this application.
   */
  public JComponent createUI() throws Exception {
    // application content
    final JComponent contentPane = createContentPane();

    // options
    JPanel colorPanel = createUserPanel(contentPane);
    colorPanel.setLocation(10, 10);
    contentPane.add(colorPanel);

    // map
    map = createMap();
    contentPane.add(map);

    return contentPane;
  }

  //------------------------------------------------------------------------
  // Private methods
  //------------------------------------------------------------------------
  private JPanel createUserPanel(final JComponent contentPane) {

    JPanel userPanel = new JPanel();
    userPanel.setLayout(new BoxLayout(userPanel, BoxLayout.Y_AXIS));
    userPanel.setSize(240, 150);

    final JLabel lblLayer = new JLabel(" Customize PointsLayer MapTip:");
    lblLayer.setMaximumSize(new Dimension(240, 25));

    final JCheckBox enableMapTip = new JCheckBox("Enable Map Tip");
    enableMapTip.setSelected(true);
    enableMapTip.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent arg0) {
        mapTip.setEnabled(enableMapTip.isSelected());
      }
    });

    final JLabel lblTipDelay = new JLabel(" Tip Delay (ms, Enter to update)");
    lblTipDelay.setMaximumSize(new Dimension(240, 20));
    final JTextField txtTipDelay = new JTextField("250", 10);
    txtTipDelay.setMaximumSize(new Dimension(120, 20));
    txtTipDelay.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent arg0) {
        map.setMapTipDelay(Integer.parseInt(txtTipDelay.getText()));
      }
    });

    final JButton btnBackground = new JButton("Select MapTip background");
    btnBackground.setBackground(Color.DARK_GRAY);
    btnBackground.setForeground(Color.WHITE);
    btnBackground.setMaximumSize(new Dimension(240, 20));
    btnBackground.setMinimumSize(new Dimension(240, 20));
    btnBackground.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent arg0) {
        Color background = JColorChooser.showDialog(
            contentPane,
            "Choose MapTip background.",
            btnBackground.getBackground());
        btnBackground.setBackground(background);
        mapTip.setBackground(background);
      }
    });

    final JButton btnForeground = new JButton("Select MapTip foreground");
    btnForeground.setBackground(Color.WHITE);
    btnForeground.setMaximumSize(new Dimension(240, 20));
    btnForeground.setMinimumSize(new Dimension(240, 20));
    btnForeground.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent arg0) {
        Color foreground = JColorChooser.showDialog(
            contentPane,
            "Choose MapTip foreground.",
            btnForeground.getBackground());
        btnForeground.setBackground(foreground);
        mapTip.setForeground(foreground);
      }
    });

    userPanel.add(lblLayer);
    userPanel.add(enableMapTip);
    userPanel.add(lblTipDelay);
    userPanel.add(txtTipDelay);
    userPanel.add(btnBackground);
    userPanel.add(btnForeground);
    userPanel.setBorder(new LineBorder(Color.BLACK, 1, false));

    return userPanel;
  }

  /**
   * Creates a window.
   * @return a window.
   */
  private JFrame createWindow() {
    JFrame window = new JFrame("MapTip 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;
  }

  /**
   * Creates a content pane.
   * @return a content pane.
   */
  private static JLayeredPane createContentPane() {
    JLayeredPane contentPane = new JLayeredPane();
    contentPane.setLayout(new BorderLayout(0, 0));
    contentPane.setVisible(true);
    return contentPane;
  }
}
Feedback on this topic?