Adding map tips

The MapTip class enables you to display information about graphics or features whenever the mouse cursor hovers over them. In your application, you can display customized map tips for each GraphicsLayer or FeatureLayer that you wish: each layer can be associated with its own MapTip object. For each MapTip object, you can set the fields that will display and customize the appearance of the map tip. You can easily disable the MapTip for a given layer such that map tips no longer display when the mouse pauses over a feature. Finally, for a given map control (JMap), you can set the amount of time (in milliseconds) that the mouse cursor has to be paused over a feature before a map tip will display.

Adding map tips to a graphics layer

A GraphicsLayer contains graphics and each of these Graphic objects has a map of attributes (key-value pairs) associated with it. These attributes are passed to the Graphic constructor when the graphic is created. Typically, a GraphicsLayer will contain graphics which all possess the same the same set of attribute keys. For example, the graphics may have come from querying a feature layer and creating graphics whose attributes are a subset of the feature layer's fields. To display selected attributes from a graphic as a map tip, you first create a map of display fields where the key String is equivalent to the key String in the attribute map, and the value is the formatted String as you wish it to display in the map tip. The MapTip object is then created with the display fields map, and set on the graphics layer, as in the code snippet below:

LinkedHashMap<String, String> displayFields = new LinkedHashMap<String, String>();
displayFields.put("Street", "Street: ");
displayFields.put("City", "City: ");
// ... add more display fields according to what your graphics' attributes are
MapTip mapTip = new MapTip(displayFields);
graphicsLayer.setMapTip(mapTip);

You can also use the parameter-less MapTip constructor, in which case no map of display fields needs to be created and only the first attribute of the graphic will display in the graphic layer's map tips.

Adding map tips to a feature layer

A feature layer is a type of graphics layer which has additional functionality to support editing, selection, definition expressions, and more. For an introduction to feature layers, please see What is a feature layer?. Adding map tips to a feature layer is done in the same way as for a graphics layer, with the additional constraint that any field you add to the display fields for the map tip needs to be a valid field in the feature layer. One way to obtain the feature layer's fields is by calling the getFields() method on the initialized layer. Because of this constraint on fields being valid for a feature layer, you need to set the map tip on the layer once it is initialized. The code snippet below shows how to do this, placing this code in a LayerInitializeCompleteListener. The code creates and sets a map tip for a feature layer which has fields named 'latitude', 'longitude', and 'datetime':

final ArcGISFeatureLayer pointsLayer = new ArcGISFeatureLayer("URL of feature layer");
map.getLayers().add(pointsLayer);
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<String, String>();
        displayFields.put("latitude", "Latitude: ");
        displayFields.put("longitude", "Longitude: ");
        displayFields.put("datetime", "Time: ");
        // create the MapTip and set it on the feature layer
        mapTip = new MapTip(displayFields);
        pointsLayer.setMapTip(mapTip);
    }
});

A feature layer usually has a default field associated with it. To display only the default field in a map tip, you can use the parameter-less MapTip constructor and avoid creating a map of display fields, as below:

// assumes 'pointsLayer' is an initialized feature layer which has a default field
MapTip mapTip = new MapTip();
pointsLayer.setMapTip(mapTip);
2/7/2013