Skip to main content

LayerBase.ViewportChanged Event

Occurs every time the current viewport is changed.

Namespace: DevExpress.XtraMap

Assembly: DevExpress.XtraMap.v23.2.dll

NuGet Package: DevExpress.Win.Map

Declaration

public event ViewportChangedEventHandler ViewportChanged

Event Data

The ViewportChanged event's data class is ViewportChangedEventArgs. The following properties provide information specific to this event:

Property Description
Angle Returns the Map Control’s rotation angle in degrees.
BottomRight Gets the bottom right coordinate visible on a map.
IsAnimated Gets whether or not the animation effect is used while the position of the current viewport is changed.
TopLeft Gets the top left coordinate visible on the map.
ZoomLevel Returns the zoom level of the current viewport.

Remarks

How to Detect Visible Items

The following example shows how to determine the number of visible vector items (in this example, map dots) on the map surface. A list box contains the coordinates of the visible items and a label displays their number. The list of visible items is re-calculated when the map viewport is changed. For example, when a user zooms the map.

A map with a list of visible dots

To do so, handle the ViewportChanged event of the layer that contains vector items. In the event handler, determine whether item coordinates are in the viewport. The viewport boundaries are defined by the e.TopLeft and e.BottomRight properties:

using DevExpress.Map;
using DevExpress.XtraMap;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace MapVisibleItems {
    public partial class Form1 : Form {
        VectorItemsLayer Layer { get { return (VectorItemsLayer)mapControl1.Layers[0]; } }
        MapItemStorage Data { get { return (MapItemStorage)Layer.Data; } }

        public Form1() {
            InitializeComponent();
            Layer.ViewportChanged += Layer_ViewportChanged;
            Data.Items.AddRange(CreateItems());
            UpdateVisibleItemsList(new GeoPoint(90, -180), new GeoPoint(-90, 180));
        }
        static bool IsPointInside(CoordPoint point, CoordPoint topLeft, CoordPoint bottomRight) {
            return (point.GetY() >= bottomRight.GetY()) && (point.GetY() <= topLeft.GetY()) &&
                   (point.GetX() >= topLeft.GetX()) && (point.GetX() <= bottomRight.GetX());
        }
        static List<MapDot> CreateItems() {
            List<MapDot> result = new List<MapDot>();
            for (double lat = -80; lat <= 80; lat += 20)
                for (double lon = -180; lon <= 180; lon += 20)
                    result.Add(new MapDot() { Location = new GeoPoint(lat, lon), Size = 15 });
            return result;
        }
        void UpdateVisibleItemsList(CoordPoint topLeft, CoordPoint bottomRight) {
            IEnumerable<CoordPoint> visibleItems = Data.Items.Select(item => ((MapDot)item).Location).
                                                              Where(location => IsPointInside(location, topLeft, bottomRight));
            listBoxControl1.Items.Clear();
            listBoxControl1.Items.AddRange(visibleItems.ToArray());
            labelControl1.Text = $"Item Count: {listBoxControl1.ItemCount.ToString()}";
        }
        void Layer_ViewportChanged(object sender, ViewportChangedEventArgs e) {
            if (!e.IsAnimated)
                UpdateVisibleItemsList(e.TopLeft, e.BottomRight);
        }
    }
}
See Also