Back to Devexpress

How to: Manually Generate Vector Items

windowsforms-17218-controls-and-libraries-map-control-examples-vector-data-providing-data-how-to-manually-generate-vector-items.md

latest4.8 KB
Original Source

How to: Manually Generate Vector Items

  • Nov 13, 2018
  • 3 minutes to read

The following example demonstrates how to create an array of vector items and display them on a map.

To show vector items on a map, do the following:

  1. Create a VectorItemsLayer and add it to the MapControl.Layers collection;
  2. Create a MapItemStorage object and assign it to the VectorItemsLayer.Data property;
  3. Generate an array of MapItem class descendants (e.g., MapCallout) and add this array to the MapItemStorage.Items property.

The code below illustrates how this can be done.

View Example

csharp
using System;
using System.Windows.Forms;
using DevExpress.XtraMap;

namespace AddItemsManually {

    public partial class Form1 : Form {

        public Form1() {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e) {
            // Create a map control with initial settings and add it to the form.
            MapControl map = new MapControl() 
                             { Dock = DockStyle.Fill, ZoomLevel = 4, CenterPoint = new GeoPoint(43, 15) };
            this.Controls.Add(map);

            // Create a layer to load image tiles from OpenStreetMap.
            ImageLayer tileLayer = new ImageLayer();
            tileLayer.DataProvider = new OpenStreetMapDataProvider();
            map.Layers.Add(tileLayer);

            // Create a layer to display vector items.
            VectorItemsLayer itemsLayer = new VectorItemsLayer();
            map.Layers.Add(itemsLayer);

            // Create a storage for map items and generate them.
            MapItemStorage storage = new MapItemStorage();
            MapItem[] capitals = GetCapitals();
            storage.Items.AddRange(capitals);
            itemsLayer.Data = storage;
        }

        // Create an array of callouts for 5 capital cities.
        MapItem[] GetCapitals() {
            return new MapItem[] { 
                new MapCallout() { Text = "London", Location = new GeoPoint(51.507222, -0.1275) },
                new MapCallout() { Text = "Rome", Location = new GeoPoint(41.9, 12.5) },
                new MapCallout() { Text = "Paris", Location = new GeoPoint(48.8567, 2.3508) },
                new MapCallout() { Text = "Berlin", Location = new GeoPoint(52.52, 13.38) },
                new MapCallout() { Text = "Madrid", Location = new GeoPoint(40.4, -3.68) }
            };
        }
    }

}
vb
Imports System
Imports System.Windows.Forms
Imports DevExpress.XtraMap

Namespace AddItemsManually

    Partial Public Class Form1
        Inherits Form

        Public Sub New()
            InitializeComponent()
        End Sub

        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
            ' Create a map control with initial settings and add it to the form.
            Dim map As New MapControl() With {.Dock = DockStyle.Fill, .ZoomLevel = 4, .CenterPoint = New GeoPoint(43, 15)}
            Me.Controls.Add(map)

            ' Create a layer to load image tiles from OpenStreetMap.
            Dim tileLayer As New ImageLayer()
            tileLayer.DataProvider = New OpenStreetMapDataProvider()
            map.Layers.Add(tileLayer)

            ' Create a layer to display vector items.
            Dim itemsLayer As New VectorItemsLayer()
            map.Layers.Add(itemsLayer)

            ' Create a storage for map items and generate them.
            Dim storage As New MapItemStorage()
            Dim capitals() As MapItem = GetCapitals()
            storage.Items.AddRange(capitals)
            itemsLayer.Data = storage
        End Sub

        ' Create an array of callouts for 5 capital cities.
        Private Function GetCapitals() As MapItem()
            Return New MapItem() { _
                New MapCallout() With {.Text = "London", .Location = New GeoPoint(51.507222, -0.1275)}, _
                New MapCallout() With {.Text = "Rome", .Location = New GeoPoint(41.9, 12.5)}, _
                New MapCallout() With {.Text = "Paris", .Location = New GeoPoint(48.8567, 2.3508)}, _
                New MapCallout() With {.Text = "Berlin", .Location = New GeoPoint(52.52, 13.38)}, _
                New MapCallout() With {.Text = "Madrid", .Location = New GeoPoint(40.4, -3.68)} _
            }
        End Function
    End Class

End Namespace