This example demonstrates how to build a list of unique values of the given field and implement zooming to them from the context menu. After the right click on the map a context menu will be displayed with names of the buildings under submenu for each letter of the alphabet. Only the buildings with unique names will be included in the list. After choosing a particular name the extents of the map will be changed to display the corresponding building. Here is a screenshot with the results of the code execution.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using AxMapWinGIS;
using MapWinGIS;
using System.Diagnostics;
namespace Examples
{
public partial class MapExamples
{
private ContextMenuStrip _menu = null;
private int _fieldIndex = -1;
public void ZoomToValues(
AxMap axMap1,
string dataPath)
{
string filename = dataPath + "buildings.shp";
if (!File.Exists(filename))
{
MessageBox.Show("Couldn't file the file: " + filename);
return;
}
sf.Open(filename, null);
m_layerHandle = axMap1.
AddLayer(sf,
true);
sf.Labels.TextRenderingHint = tkTextRenderingHint.SystemDefault;
_fieldIndex = sf.Table.FieldIndexByName["Name"];
var names = new Dictionary<string, int>();
for (int i = 0; i < sf.NumShapes; i++)
{
string name = (string)sf.Table.CellValue[_fieldIndex, i];
if (names.ContainsKey(name))
{
names[name]++;
}
else
{
names.Add(name, 1);
}
}
IEnumerable<string> list = names.Where(val => val.Value == 1).Select(val => val.Key);
_menu = new ContextMenuStrip();
string s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (int i = 0; i < s.Length; i++)
{
ToolStripMenuItem item = new ToolStripMenuItem();
item.Name = s[i].ToString();
item.Text = s[i].ToString();
_menu.Items.Add(item);
}
foreach (string name in list)
{
string ch = name[0].ToString();
foreach (ToolStripMenuItem item in _menu.Items)
{
if (item.Name == ch)
{
ToolStripMenuItem subItem = new ToolStripMenuItem();
subItem.Click += new EventHandler(ItemClick);
subItem.Text = name;
subItem.Tag = "zoomable";
item.DropDownItems.Add(subItem);
break;
}
}
}
for (int i = _menu.Items.Count - 1; i >= 0; i--)
{
ToolStripMenuItem item = _menu.Items[i] as ToolStripMenuItem;
if (item.DropDownItems.Count == 0)
_menu.Items.Remove(item);
}
MapEvents.MouseDownEvent += AxMap1MouseDownEvent4;
}
private void AxMap1MouseDownEvent4(object sender, _DMapEvents_MouseDownEvent e)
{
if (e.button == 2)
{
System.Drawing.Point pnt = axMap1.PointToScreen(new System.Drawing.Point(e.x, e.y));
_menu.Show(pnt.X, pnt.Y);
}
}
private void ItemClick(object sender, EventArgs e)
{
ToolStripMenuItem item = (sender as ToolStripMenuItem);
if (item == null) return;
if (item.Tag.ToString() == "zoomable")
{
if (sf != null)
{
{
Debug.Print(s);
if (s == item.Text)
{
break;
}
}
}
}
}
}
}
tkLabelPositioning
The available positioning of the label relative to the parent shape.
Definition: Enumerations.cs:835
tkCursorMode
Available cursor modes. Determines the default respond of map to the action of user.
Definition: Enumerations.cs:344
tkMapProjection
Commonly used map projections to be set in Form Designer (see AxMap.Projection property).
Definition: Enumerations.cs:1741
tkUnitsOfMeasure
The possible units of measure for the data being displaying on map.
Definition: Enumerations.cs:1397
Map component for visualization of vector, raster or grid data.
Definition: AxMap.cs:56
Provides a functionality for accessing and editing ESRI shapefiles.
Definition: Shapefile.cs:72
Labels Labels
Gets or sets the instance of the Labels class associated with the shapefile.
Definition: Shapefile.cs:184
int NumShapes
Gets the number of shapes in the shapefile.
Definition: Shapefile.cs:254
int Generate(string Expression, tkLabelPositioning Method, bool LargestPartOnly)
Generates labels for each shape of the parent shapefile.
Definition: Labels.cs:490
tkUnitsOfMeasure MapUnits
Gets or sets the units of measure for the map.
Definition: AxMap.cs:2622
tkMapProjection Projection
Sets projection of the map. It providers 2 most commonly used coordinate system/projections to be eas...
Definition: AxMap.cs:2709
double CurrentScale
Gets or sets the current map scale.
Definition: AxMap.cs:2395
tkCursorMode CursorMode
Gets or sets the cursor mode for the map.
Definition: AxMap.cs:456
void ZoomToShape(int layerHandle, int shape)
Zooms the map display to the specified shape in the shapefile contained by the specified layer.
Definition: AxMap.cs:689
bool SendMouseDown
Gets or sets whether the map sends mouse down events.
Definition: AxMap.cs:553
int AddLayer(object Object, bool visible)
Adds a layer to the map.
Definition: AxMap.cs:1342
Shapefile get_Shapefile(int layerHandle)
Gets shapefile object associated with the layer.
Definition: AxMap.cs:1546
object get_CellValue(int fieldIndex, int shapeIndex)
Gets the value of the specified field for the shape.
Definition: Shapefile.cs:601