Provide Data Using Vector Item Attributes
- 4 minutes to read
Attributes are used to associate a Vector Item with supplementary information. The Map Control operates with vector item attributes, allowing you to visualize any kind of data.
A single attribute is represented by the MapItemAttribute object. This object can be accessed as an item of the MapItemAttributeCollection, returned by the MapItem.Attributes property of the MapItem class descendant.
These attributes can be specified in various ways, depending on the approach the Map Control utilizes to obtain vector items. The sections below describe each approach in detail, as well as how to use vector item attributes.
Manual Creation of Vector Items
When vector items are created manually, you can also specify the attributes for any vector item.
To create an attribute:
- Create a MapItemAttribute object and add it to the collection returned by the MapItem.Attributes property;
- Specify the attribute name, type, and value using the MapItemAttribute.Name, MapItemAttribute.Type, and MapItemAttribute.Value properties.
The following code shows how this can be done for the Map Polygon.
MapPolygon item = new MapPolygon();
item.Attributes.Add(new MapItemAttribute() { Name = areaValueAttrName, Type = typeof(double), Value = areaValue });
Loading Vector Items from a Shapefile
When the map control obtains vector items from a vector formatted file using ShapefileDataAdapter, KmlFileDataAdapter or SqlGeometryDataAdapter, vector item attributes are generated automatically.
The following code demonstrates how to load data from a Shapefile.
const string shapefilePath = "../../Data/Countries.shp";
VectorItemsLayer vectorLayer = new VectorItemsLayer() {
Data = LoadDataFormShapefile(shapefilePath)
}
private MapDataAdapterBase LoadDataFromShapefile(String filename) {
Uri baseUri = new Uri(System.Reflection.Assembly.GetEntryAssembly().Location);
// Create an adapter to load data from shapefile.
ShapefileDataAdapter adapter = new ShapefileDataAdapter() {
FileUri = new Uri(baseUri, filename)
};
return adapter;
}
In this case, you have the capability to select attributes to be used by the Map Control to visualize supplementary data. For example, the code snippet below shows how to display Name, Year and Desc attributes in tooltips for vector items.
Loading Vector Items from a Data Source
If the Map Control obtains vector items from a data source, the attributes are created by mapping data source fields to vector item attributes.
As a result of vector item mapping, the map control specifies which data field from the data source will be bound to which data member (DataSourceAdapterBase.DataMember) of the DataSourceAdapterBase class descendant object. This object can be accessed using the VectorItemsLayer.Data property.
To create vector item attributes, do the following:
- Create attribute mappings using the MapItemAttributeMapping.Name, MapItemAttributeMapping.Member and MapItemAttributeMapping.Type properties of the MapItemAttributeMapping object.
- Add each attribute mapping to the MapItemAttributeMappingCollection object with the DataSourceAdapterBase.AttributeMappings property.
The code below shows how to create Name, Year and Desc attributes, and add them to the map item attribute mapping collection.
VectorItemsLayer itemsLayer = new VectorItemsLayer();
ListSourceDataAdapter dataAdapter = new ListSourceDataAdapter();
// ...
itemsLayer.Data = dataAdapter;
// Specify main mappings and additional mappings.
dataAdapter.Mappings.Latitude = "Latitude";
dataAdapter.Mappings.Longitude = "Longitude";
dataAdapter.AttributeMappings.Add(new MapItemAttributeMapping("Name", "Name"));
dataAdapter.AttributeMappings.Add(new MapItemAttributeMapping("Year", "Year"));
dataAdapter.AttributeMappings.Add(new MapItemAttributeMapping("Description", "Desc"));
Using Vector Item Attributes
There are different ways to use vector item attributes in your application.
The most popular use of vector item attributes is when showing vector data from a datasource in tooltips. To learn how this can be done, see the How to: Automatically Generate Vector Items from a Datasource example.
Another example is to color shape contours depending on data associated with a shape. To do this, use the Map Colorizer. See the How to: Colorize Manually Added Shapes Using the Choropleth Colorizer example to learn more.