Skip to content

MapView and Render Modes (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

MapView is the Blazor component that puts a map on a page. This guide starts where the Web Maps Quick Start ends. It covers how a MapView is declared, where its code runs in each render mode, when the map is ready to be called, and what to check when a map stays empty.

Every example on this page uses a Blazor Web App with Server as the interactive render mode and the two namespaces in _Imports.razor, as the Quick Start sets them up.


Declaring the map

<MapView Id="map" MapUnit="GeographyUnit.Meter"
         Center="@(new PointShape(0, 2000000))" Zoom="2" Width="900" Height="600">
    <OverlaysSetting>
        <ThinkGeoCloudVectorMapsOverlay Id="basemap" MapType="ThinkGeoCloudVectorMapsMapType.Light"
                                        ApiKey="SGjwpxF60knqZi3R0A9RRE3GqiuRA1LsnxHMot7rJ58~" />
    </OverlaysSetting>
</MapView>
Parameter What it does
Id The id of the map's element on the page. Two maps on one page need two ids.
MapUnit The unit of every coordinate the map takes and gives back. GeographyUnit.Meter is Web Mercator (EPSG:3857), the projection of the ThinkGeo Cloud basemaps and of every other web tile service.
Center, Zoom Where the map starts, in MapUnit. They are read once, when the map is created; to move the map afterwards, call its methods.
Width, Height The map's size, in pixels. With MapViewSizeUnitType="MapViewSizeUnitType.Percentage" they are percentages of the element around the map, which must then have a height of its own.
MinimumScale, MaximumScale, RestrictExtent How far in and out, and where, the user can go. Applied when the map is created.
Pitch, Bearing Tilt and rotation. Unlike Center and Zoom, the map follows these: change the value and the map tilts or turns.
IsRotationEnabled, IsTiltEnabled Whether the user can turn and tilt the map with the mouse, touch and keyboard. Both are on by default, and the map follows them. Turned off, code can still turn and tilt the map through Bearing and Pitch. On MapLibre, the right-button drag and Shift + arrow keys turn and tilt together, so they go off when either is off. Only MapLibre can tilt a map.
Renderer Which browser library draws the map. Leave it at Auto: MapLibre when everything is in Web Mercator, OpenLayers when anything is not.

The overlays go inside OverlaysSetting and are drawn in the order they are declared, the first at the bottom. Overlays and RedrawAsync covers them.


Where the code runs

A Blazor page runs its C# code either on the server or in the browser. The map itself is always drawn in the browser; what the render mode decides is where your code, and the FeatureSources it opens, run.

Render mode Where your C# code runs Where the map's data is read
Interactive Server (@rendermode InteractiveServer) On the server. The browser and the server talk over a SignalR connection, the circuit. On the server. Files in the project are read where they are.
Interactive WebAssembly, and a standalone Blazor WebAssembly app In the browser, on .NET compiled to WebAssembly. In the browser. It cannot read the server's files; the application downloads them first.
Interactive Auto On the server for the first visit, in the browser once the WebAssembly files are downloaded. Both, so the page must work in both places.

A page without a render mode is static: it is sent as HTML and nothing runs afterwards, so the map stays empty. In a Blazor Web App every page that has a map needs a @rendermode line, or the render mode must be set for the whole app in App.razor.

Blazor WebAssembly covers what changes in the browser. Interactive Auto has not been tested with the examples in these guides; a page written for it must load its data in a way that works both on the server and in the browser.


When the map is ready

The map is created in the browser after the page first renders, and its overlays are attached one by one. Two members tell you when that is done:

  • WhenReady is a task that completes once the map and every overlay declared in it are in the browser.
  • OnReady is an event raised once, at the same moment.

The methods of MapView itself, such as ZoomToAsync, SetCenterAsync and GetCurrentExtentAsync, wait for WhenReady on their own, so they can be called at any time. The RedrawAsync of an overlay does not wait: called before the map is ready it throws InvalidOperationException with the message Invalid operation. The Overlay is not initialized yet. Await map.WhenReady first when the call can come early, for example from OnAfterRenderAsync(firstRender) or from a timer.

Example: a button that moves the map

@page "/mapview"
@rendermode InteractiveServer

<button @onclick="GoToParisAsync" disabled="@(!ready)">Zoom to Paris</button>
<span>@status</span>

<MapView @ref="map" Id="map" MapUnit="GeographyUnit.Meter"
         Center="@(new PointShape(0, 2000000))" Zoom="2" Width="900" Height="600"
         OnReady="@OnMapReady" OnCurrentExtentChanged="@OnExtentChanged">
    <OverlaysSetting>
        <ThinkGeoCloudVectorMapsOverlay Id="basemap" MapType="ThinkGeoCloudVectorMapsMapType.Light"
                                        ApiKey="SGjwpxF60knqZi3R0A9RRE3GqiuRA1LsnxHMot7rJ58~" />
    </OverlaysSetting>
</MapView>

@code {
    private MapView map = default!;
    private bool ready;
    private string status = "Loading the map…";

    private void OnMapReady()
    {
        // The map is in the browser and its overlays are attached: it can take calls now.
        ready = true;
        status = "Ready";
    }

    private async Task GoToParisAsync()
    {
        // Paris in Web Mercator, the map's unit.
        await map.ZoomToAsync(new PointShape(261600, 6250000), 100000);
    }

    private void OnExtentChanged(CurrentExtentChangedMapViewEventArgs e)
    {
        // The extent is in the map's unit, meters here.
        status = $"{e.CurrentExtent.Width / 1000:N0} km across";
    }
}

@ref="map" gives the code the component. The field is assigned when the page first renders, so it is still null in OnInitialized; use it from event handlers, from OnReady, or after WhenReady.


Prerendering

A Blazor Web App prerenders interactive pages by default: the server first sends the page as plain HTML, then the page starts again as an interactive component. The map is created only in the interactive run, so prerendering does no harm to it, but OnInitialized and OnInitializedAsync run twice, once in each run. Code there that is slow or has side effects, such as reading a large file or writing to a database, runs twice as well.

To prerender nothing on a map page, set the render mode with prerendering off:

@rendermode @(new InteractiveServerRenderMode(prerender: false))

Leaving the page

When the user navigates away, Blazor disposes the page, and the MapView with it. The map removes itself from the browser and releases its overlays; there is nothing to clean up in your code. When the user comes back, the page and the map are created again from the start. State that must outlive a visit, such as the markers a user placed, belongs in a service rather than in the page.


When the map is empty

What you see Usually because What to do
Nothing where the map should be, and buttons do nothing The page has no interactive render mode. Add @rendermode InteractiveServer, or set it for the app.
The map's area is empty with a percentage size The element around the map has no height, so 100% of it is 0. Give that element a height, or use a size in pixels.
The basemap is empty The Cloud key is missing or wrong, or the browser cannot reach ThinkGeo Cloud. Check the browser's developer tools for failed tile requests.
Your data is missing over a basemap that is drawn Coordinates in another projection, a style source-layer that does not match, or a file that is not where the code looks. VectorTileOverlay and Style Documents.
Invalid operation. The Overlay is not initialized yet. RedrawAsync was called before the map was ready. await map.WhenReady first.

See also