Events, Markers and Popups (Blazor)¶
Namespace: ThinkGeo.UI.Blazor
Applies to: ThinkGeo Web Maps, Blazor on .NET 8 or later
Tested with: ThinkGeo.UI.Blazor 15.1.0, .NET 10, Chrome
Overview¶
This guide makes a map interactive: it handles clicks on the map, places markers where the user clicks, opens a popup on the marker the user picks, and keeps a panel next to the map in step with it. It assumes a map set up as in MapView and Render Modes.
Map events¶
MapView raises its events as Blazor EventCallbacks, so a handler can be synchronous or async, and the page renders again after it returns.
| Event | Arguments | Raised |
|---|---|---|
OnClick | ClickedMapViewEventArgs | on a click |
OnDoubleClick | DoubleClickedMapViewEventArgs | on a double-click, which also zooms in |
OnMouseMove | MouseMovingMapViewEventArgs | as the pointer moves; IsDragging says whether a button is down |
OnCurrentExtentChanged | CurrentExtentChangedMapViewEventArgs | once the map has stopped moving |
OnCurrentExtentChangedInAnimation | CurrentExtentChangedInAnimationMapViewEventArgs | while the map moves, about 20 times a second |
OnSizeChanged | SizeChangedMapViewEventArgs | when the map's element changes size |
OnReady | none | once, when the map is ready (MapView and Render Modes) |
Declare the handler in the map's tag. The click, double-click, mouse-move and extent events are wired in the browser when the map is created, so a handler that is first given to the map later is not called.
Under Interactive Server, every event is a message from the browser to the server and back. OnMouseMove and OnCurrentExtentChangedInAnimation send many of them; subscribe only when the page needs them, and keep their handlers short.
Coordinates¶
WorldX and WorldY are in the map's unit, the same unit as Center, a marker's Position and the features of your overlays: Web Mercator meters with MapUnit="GeographyUnit.Meter". ScreenX and ScreenY are pixels from the top left corner of the map.
To show a position as longitude and latitude, or to compare it with data stored in them, convert it with a ProjectionConverter:
private readonly ProjectionConverter toLonLat = new(3857, 4326);
private string ToLonLat(PointShape position)
{
if (!toLonLat.IsOpen) toLonLat.Open();
var point = (PointShape)toLonLat.ConvertToExternalProjection(position);
return $"{point.Y:F4}, {point.X:F4}";
}
The ProjectionConverter Guide explains the converter.
Markers¶
A marker is an image pinned to a position on the map, drawn by the browser above every overlay. Markers belong to a SimpleMarkerOverlay, declared in the map's MarkerOverlaySetting. The overlay's Markers collection holds them, keyed by each marker's Id.
<MarkerOverlaySetting>
<SimpleMarkerOverlay @ref="pins" Id="pins" OnClick="@OnMarkerClick" />
</MarkerOverlaySetting>
Adding or removing a marker changes a collection, not a parameter, so it is followed by RedrawAsync, as for any overlay (Two kinds of change):
var marker = new Marker { Id = "Depot", Position = new PointShape(261600, 6250000) };
pins.Markers.Add(marker.Id, marker);
await pins.RedrawAsync();
Marker is a Blazor component, and Blazor's analyzer warns (BL0005) when code sets a component's parameters. For markers made in code that is safe; silence the warning with #pragma warning disable BL0005 at the top of the @code block.
Without a Style, a marker is the bundled pin, anchored at its bottom centre. new PointMarkerStyle("images/truck.svg", 0.5, 0.5) draws an image from wwwroot instead, anchored at its centre.
The overlay's OnClick reports the marker that was clicked, in ClickedMarkerOverlayEventArgs.ClickedMarker. A marker's own OnClick gives the position of the click as well.
Markers can also come from a collection in markup, one Marker per item of MarkerSource. This suits markers that follow a list the page already has:
<SimpleMarkerOverlay Id="hotels" MarkerSource="@hotels">
<MarkersSetting Context="hotel">
<Marker Id="@(((Hotel)hotel).Name)" Position="@(((Hotel)hotel).Position)" />
</MarkersSetting>
</SimpleMarkerOverlay>
Popups¶
A popup is a box of HTML pinned to a position on the map. Popups are declared in a PopupOverlay, in the map's PopupOverlaySetting, and their content is ordinary Razor markup, so it can show anything the page can.
A popup is on the map for as long as it is in the markup. To open and close one, put it in an @if:
<PopupOverlaySetting>
<PopupOverlay Id="popups">
<PopupSetting>
@if (selected != null)
{
<Popup Id="details" Position="@selected.Position" OffsetY="-36" OnClose="@(() => selected = null)">
<b>@selected.Id</b><br />@ToLonLat(selected.Position)
</Popup>
}
</PopupSetting>
</PopupOverlay>
</PopupOverlaySetting>
Position is in the map's unit. OffsetX and OffsetY move the popup by pixels, here up by the height of the pin. The popup's corner button raises OnClose; set the field the @if reads, and the popup leaves the map when the page renders.
Unlike markers, popups need no RedrawAsync: they are Blazor content, and follow their parameters.
Example: click to add, select to see details¶
Click the map to add a marker. Click a marker to open its popup and show it in the panel next to the map, which also removes it.
@page "/markers"
@rendermode InteractiveServer
<div style="display:flex;gap:16px">
<MapView @ref="map" Id="map" MapUnit="GeographyUnit.Meter"
Center="@(new PointShape(0, 2000000))" Zoom="2" Width="900" Height="600"
OnClick="@OnMapClickAsync">
<OverlaysSetting>
<ThinkGeoCloudVectorMapsOverlay Id="basemap" MapType="ThinkGeoCloudVectorMapsMapType.Light"
ApiKey="SGjwpxF60knqZi3R0A9RRE3GqiuRA1LsnxHMot7rJ58~" />
</OverlaysSetting>
<MarkerOverlaySetting>
<SimpleMarkerOverlay @ref="pins" Id="pins" OnClick="@OnMarkerClick" />
</MarkerOverlaySetting>
<PopupOverlaySetting>
<PopupOverlay Id="popups">
<PopupSetting>
@if (selected != null)
{
<Popup Id="details" Position="@selected.Position" OffsetY="-36" OnClose="@(() => selected = null)">
<b>@selected.Id</b><br />@ToLonLat(selected.Position)
</Popup>
}
</PopupSetting>
</PopupOverlay>
</PopupOverlaySetting>
</MapView>
<div>
<h5>Markers</h5>
<p>Click the map to add a marker, then click a marker.</p>
<p>Selected: @(selected?.Id ?? "none")</p>
<button @onclick="RemoveSelectedAsync" disabled="@(selected == null)">Remove it</button>
</div>
</div>
@code {
// Markers made in code: Blazor warns (BL0005) about setting a component's parameters, which is safe here.
#pragma warning disable BL0005
private MapView map = default!;
private SimpleMarkerOverlay pins = default!;
private Marker? selected;
private int next = 1;
// The map works in Web Mercator meters; people read longitude and latitude.
private readonly ProjectionConverter toLonLat = new(3857, 4326);
private async Task OnMapClickAsync(ClickedMapViewEventArgs e)
{
// WorldX and WorldY are in the map's unit, the same as a marker's position.
var marker = new Marker { Id = $"Marker {next++}", Position = new PointShape(e.WorldX, e.WorldY) };
pins.Markers.Add(marker.Id, marker);
await pins.RedrawAsync();
}
private void OnMarkerClick(ClickedMarkerOverlayEventArgs e)
{
// Setting a field re-renders the page: the popup and the panel follow it.
selected = e.ClickedMarker;
}
private async Task RemoveSelectedAsync()
{
if (selected == null) return;
pins.Markers.Remove(selected.Id);
selected = null;
await pins.RedrawAsync();
}
private string ToLonLat(PointShape position)
{
if (!toLonLat.IsOpen) toLonLat.Open();
var point = (PointShape)toLonLat.ConvertToExternalProjection(position);
return $"{point.Y:F4}, {point.X:F4}";
}
}
The map, the popup and the panel all render from one field, selected. That is the pattern for anything the map and the rest of the page share: keep it in a field, change the field in the handler, and let the page render.
Features instead of markers¶
Markers suit tens or a few hundred points a user works with one by one. For thousands of points, or points that are data rather than places the user picked, draw them with a VectorTileOverlay and a style document instead (VectorTileOverlay and Style Documents). To find the feature under a click, query the source on the server with the click's WorldX and WorldY, for example with GetFeaturesNearestTo, and open a popup at the feature's position.
Drawing and editing shapes with the EditOverlay is not covered in these guides; the Draw & Edit pages of the Blazor How Do I samples show it.