Skip to content

Adding Features to datasource

Compare
Choose a tag to compare
@arnaudleclerc arnaudleclerc released this 24 Mar 11:19
· 164 commits to develop since this release

Two new signatures for the AddAsync method on DataSource:

  • Task AddAsync(IEnumerable<Feature> features)
  • Task AddAsync(params Feature[] features)

The calls to AddAsync now support adding geometries which are wrapped into shapes, but also adding features containing a geometry. The added features are accessible via the Features property of the datasource.

@page "/SymbolLayerWithFeatures"

@using AzureMapsControl.Components.Map
<AzureMap Id="map"
          CameraOptions="new CameraOptions { Zoom = 16, Center = new Components.Atlas.Position(-122.13284, 47.63699) }"
          EventActivationFlags="MapEventActivationFlags
                                .None()
                                .Enable(MapEventType.Ready)"
          OnReady="OnMapReady" />
i
@code  {
    public async Task OnMapReady(MapEventArgs eventArgs)
    {
        var datasourceId = "datasourceId";
        var datasource = new AzureMapsControl.Components.Data.DataSource(datasourceId);
        await eventArgs.Map.AddSourceAsync(datasource);

        var feature = new AzureMapsControl.Components.Atlas.Feature<AzureMapsControl.Components.Atlas.Point>(new AzureMapsControl.Components.Atlas.Point(
                    new AzureMapsControl.Components.Atlas.Position(-122.13284, 47.63699)
                ), new Dictionary<string, object> {
                    { "title", "Cafeteria" },
                    { "subtitle", "Building 40"}
                });

        await datasource.AddAsync(feature);

        var textFieldExpressionJsonString = "[\"format\", [\"get\", \"title\"], {\"text-font\": [\"literal\", [\"StandardFont-Bold\"]], \"font-scale\": 1.25}, \"\\n\", {}, [\"get\", \"subtitle\"], {\"font-scale\": 0.75}]";

        var layer = new AzureMapsControl.Components.Layers.SymbolLayer {
            Options = new Components.Layers.SymbolLayerOptions {
                Source = datasourceId,
                TextOptions = new Components.Layers.TextOptions {
                    TextField = new Components.Atlas.ExpressionOrString(
                        System.Text.Json.JsonDocument.Parse(textFieldExpressionJsonString)
                    )
                }
            }
        };

        await eventArgs.Map.AddLayerAsync(layer);
    }
}

Removing a feature is possible by calling the RemoveAsync method of the datasource, just like for the geometries. Some new API are available to remove only geometries, only features or both of them.