Changing the Map While It Runs¶
A style document is not fixed once it is on the map. This page covers the ways a running map changes: switching layers, restyling, layers built in code with C# paint, features that move, and data that changed underneath. They differ in cost, so it starts with that.
Three speeds¶
| Change | What happens | Cost |
|---|---|---|
style.SetLayerVisibility(id, visible), style.SetHeatmapPaint(id, …) | Takes effect on the next frame. Nothing is rebuilt. | Free |
await Map.Basemap.SetStyleAsync(newStyle) | The new document is compiled and the map is rebuilt from the tiles it already holds. Nothing is downloaded or cut again for a source it recognizes. | A moment |
Map.Basemap = new GpuBasemap(style) | A new map from an empty scene: every source is opened and every tile fetched again. | A blank flash |
Reach for the cheapest one that does the job. Replacing the basemap is for a different map, not a different look.
Restyling¶
Build a new MapStyle over the same source objects and hand it to SetStyleAsync. The sources are what the map recognizes; a new FeatureSourceVectorTileSource over the same file is a new source, and its tiles are cut again.
private FeatureSourceVectorTileSource _tiles; // created once, passed with every document
private async Task ApplyAsync(string json)
{
try
{
await Map.Basemap.SetStyleAsync(new MapStyle(json, _tiles));
}
catch (Exception exception)
{
Status.Text = "not applied - " + exception.Message; // the map keeps the last document that worked
}
}
The HowDoI style editors do exactly this, 400 ms after the last keystroke.
Editing the style the map already holds works too: every Add… and Set… on an attached MapStyle schedules the restyle for you, debounced, so a loop of changes is applied once.
Switching layers on and off¶
SetLayerVisibility hides or shows any layer of the document by id, at no cost. StyleLayers lists the layers in style order once the map has compiled the document, with each layer's Id and Type, which is enough for a layer list with checkboxes:
var style = new MapStyle(ThinkGeoVectorStyles.Dark, cloud);
Map.Basemap = new GpuBasemap(style);
await Map.RefreshAsync();
LayerList.ItemsSource = style.StyleLayers.ToList(); // StyleLayer.Id, StyleLayer.Type
// in the checkbox handler
style.SetLayerVisibility(layer.Id, box.IsChecked == true);
Layers you add yourself get the id you give them, so a match with five classes can also be five layers with filters, switched one at a time.
Layers built in code¶
A document can hold layers written in C# instead of JSON. StyleLayer.CreateFill, CreateLine, CreateCircle, CreateSymbol, CreateFillExtrusion and CreateHeatmap take a paint delegate and an optional filter delegate, both with the feature and the zoom as arguments, so the column name is checked by the compiler and the logic can be anything C# can say:
var states = new FeatureSourceVectorTileSource();
states.FeatureSources.Add("states", new ShapeFileFeatureSource(@"Data\USStates_3857.shp"));
var style = new MapStyle(ThinkGeoVectorStyles.Light, cloud);
style.AddVectorTiles(states, "usstates", StyleLayerSlot.AboveFills,
StyleLayer.CreateFill("states-fill", "states",
paint: (feature, _) => new FillPaint(DensityColor(feature), opacity: 0.60f)),
StyleLayer.CreateLine("states-outline", "states",
paint: (_, zoom) => new LinePaint(GeoColors.White, zoom < 5d ? 0.6f : 1.4f, opacity: 0.9f)));
One rule to know: a paint delegate runs when the tile is built, not on every frame. What it returns for a feature is baked into that tile. A delegate that reads the clock freezes at the hour each tile was cut. For state that changes over time, build the variants as separate layers and switch their visibility, or use a DynamicSource. The HowDoI sample Render Based on Code does both: a pair of day and night layers per time zone switched by the clock, and a choropleth painted by a lambda.
Features that move¶
Vehicles, aircraft, a growing track: a DynamicSource (namespace ThinkGeo.Gpu) is a source whose features you set one at a time, by id, and the map draws the current state every frame.
using ThinkGeo.Gpu;
var live = new DynamicSource(new Projection(4326)); // the projection the coordinates arrive in
style.AddDynamic(live);
Map.Basemap = new GpuBasemap(style);
// on every position report
live.SetPoint(flight.Id, new Feature(flight.Longitude, flight.Latitude), headingInDegrees: flight.Heading, iconName: "plane");
live.SetLine(flight.Id + "-trail", new Feature(trail));
SetPoint, SetLine, SetArea and SetLabel replace the feature of that id; Remove(id) and Clear() take them away. Icons come from style.Images. The HowDoI samples Live Flights and Vehicle Navigation are built on it.
Heatmaps¶
A heatmap layer's radius, intensity, opacity and color ramp are the paint that changes most while a user explores, so they have a method of their own that takes effect on the next frame:
style.SetHeatmapPaint("coyote-heat", radius: 30, intensity: 1.2f, opacity: 0.8f, colorStops: ramp);
Data that changed¶
When the features behind a FeatureSourceVectorTileSource changed on disk or in the database, ask the map to cut that source again, for the whole map or for one extent:
Map.Basemap.RefreshVectorTiles(tiles);
Map.Basemap.RefreshVectorTiles(tiles, changedExtent);
For an InMemoryFeatureSource you edit in code, a handful of features that change often is a DynamicSource; a large set that changes now and then is a refresh.
Replacing the basemap¶
Assigning Map.Basemap starts from an empty scene. It is the right call when the map really is a different map, an archive of another region, a different provider, and the HowDoI sample Offline Tiles does it for that reason. For a different look over the same data, restyle instead.
A basemap that is replaced is handed back to you, not disposed: dispose it once the new one is on the map. The sources you gave it stay open, for the new basemap or for you to dispose. See Who disposes the sources.