windowsforms-devexpress-dot-xtramap-dot-layerbase-b570120b.md
Occurs every time the current viewport is changed.
Namespace : DevExpress.XtraMap
Assembly : DevExpress.XtraMap.v25.2.dll
NuGet Package : DevExpress.Win.Map
public event ViewportChangedEventHandler ViewportChanged
Public Event ViewportChanged As ViewportChangedEventHandler
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. |
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.
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);
}
}
}
Imports DevExpress.Map
Imports DevExpress.XtraMap
Imports System.Collections.Generic
Imports System.Linq
Imports System.Windows.Forms
Namespace MapVisibleItems
Public Partial Class Form1
Inherits Form
Private ReadOnly Property Layer As VectorItemsLayer
Get
Return CType(mapControl1.Layers(0), VectorItemsLayer)
End Get
End Property
Private ReadOnly Property Data As MapItemStorage
Get
Return CType(Layer.Data, MapItemStorage)
End Get
End Property
Public Sub New()
InitializeComponent()
Layer.SelectedItemStyle.Fill = Drawing.Color.Red
Layer.SelectedItemStyle.StrokeWidth = 10
Layer.SelectedItemStyle.Stroke = Drawing.Color.Red
Me.Layer.ViewportChanged += AddressOf Layer_ViewportChanged
Data.Items.AddRange(CreateItems())
UpdateVisibleItemsList(New GeoPoint(90, -180), New GeoPoint(-90, 180))
End Sub
Private Shared Function IsPointInside(ByVal point As CoordPoint, ByVal topLeft As CoordPoint, ByVal bottomRight As CoordPoint) As Boolean
Return (point.GetY() >= bottomRight.GetY()) AndAlso (point.GetY() <= topLeft.GetY()) AndAlso (point.GetX() >= topLeft.GetX()) AndAlso (point.GetX() <= bottomRight.GetX())
End Function
Private Shared Function CreateItems() As List(Of MapDot)
Dim result As List(Of MapDot) = New List(Of MapDot)()
For lat As Double = -80 To 80 Step 20
For lon As Double = -180 To 180 Step 20
result.Add(New MapDot() With {
.Location = New GeoPoint(lat, lon),
.Size = 15
})
Next
Next
Return result
End Function
Private Sub UpdateVisibleItemsList(ByVal topLeft As CoordPoint, ByVal bottomRight As CoordPoint)
Dim visibleItems As IEnumerable(Of CoordPoint) = Data.Items.[Select](Function(item) CType(item, MapDot).Location).Where(Function(location) IsPointInside(location, topLeft, bottomRight))
listBoxControl1.Items.Clear()
listBoxControl1.Items.AddRange(visibleItems.ToArray())
labelControl1.Text = $"Item Count: {listBoxControl1.ItemCount.ToString()}"
End Sub
Private Sub Layer_ViewportChanged(ByVal sender As Object, ByVal e As ViewportChangedEventArgs)
If Not e.IsAnimated Then UpdateVisibleItemsList(e.TopLeft, e.BottomRight)
End Sub
End Class
End Namespace
See Also