Displaying MapTips in ArcGIS Mobile

Using labels on maps can be a good way to identify features and provide extra information, but this can result in maps looking cluttered or data being covered up. Using MapTips, which appear when the mouse cursor passes over or next to a feature, are a great way to maximize the information for users and minimize the map clutter.

The code below shows how easy it is to declare and use the MapTips class once it has been added to your solution.

mapToolTip = new MapToolTip(); 
private void map1_MouseMove(object sender, MapMouseEventArgs e) 
{ 
  if (showMapToolTip) 
  {
    mapToolTip.DrawMapToolTip(map1, e.MapCoordinate);
  }
} 
private void TiplayersComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
  string selectedlayername = TiplayersComboBox.SelectedItem.ToString(); 
  mapToolTip.FeatureLayer = mobileCache1.Layers[selectedlayername] as FeatureLayer;
  displayColumns = new int[] { 0, 1, 2, 3 };
  mapTipFormat = TiplayersComboBox.SelectedItem.ToString() + ": {0}\n{1}\n{2}\n{3}";
  mapToolTip.DisplayColumnIndices = displayColumns; 
  mapToolTip.CustomFormatter = mapTipFormat; 
}

The real work in making the MapTips display is contained in the MapTip class. The code shown below is responsible for the actual MapTip drawing on the map display with the correct information.

public void DrawMapToolTip(Map map, Coordinate coordinate) 
{
  if (m_featureLayer == null | coordinate == null | m_displayColumnIndices == null)
    return; 
  Envelope env = new Envelope(coordinate, map.ToMap(m_mapTolerance), map.ToMap(m_mapTolerance)); 
  switch (m_featureLayer.GeometryType) 
  { 
  case GeometryType.Polygon: 
  case GeometryType.Envelope:
    m_queryFilter = new QueryFilter(new ESRI.ArcGIS.Mobile.Geometries.Point(coordinate), GeometricRelationshipType.Within, null, true);
    break;
  case GeometryType.Point: 
  case GeometryType.Multipoint:
  case GeometryType.Polyline:
    m_queryFilter = new QueryFilter(env, GeometricRelationshipType.Intersect, null, true);
    break;
  }
  string[] textToDisplay = new string[m_displayColumnIndices.GetLength(0)];
  string textToDisplayDefault = "";
  FeatureDataReader fdr = m_featureLayer.GetDataReader(m_queryFilter); 
  if (fdr.Read()) 
  {
    for (int i = 0; i < m_displayColumnIndices.GetLength(0); i++)
    {
      textToDisplay[i] = fdr[m_displayColumnIndices[i]].ToString();
      textToDisplayDefault += fdr[m_displayColumnIndices[i]].ToString() + " ";
    }
    if (m_customFormatter != null && m_customFormatter != "") 
      this.SetToolTip(map, string.Format(m_customFormatter, textToDisplay));
    else 
      this.SetToolTip(map, textToDisplayDefault.Trim()); 
    } 
  else 
  {
    this.SetToolTip(map, ""); 
  }
  fdr.Close(); 
  }
}

Where to get the sample

The sample is available for download from here.


9/20/2011