Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Community.Blazor.MapLibre/Community.Blazor.MapLibre.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
<None Include="..\README.md" Pack="true" PackagePath="\"/>
<Content Remove="libman.json" />
<None Include="libman.json" />
<Content Update="wwwroot\terra-draw\dist\TerraDrawCoordinateDeleteMode.js">
<PublishFolderType>Content</PublishFolderType>
</Content>
Comment on lines +38 to +40

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tror det här inte behövs

</ItemGroup>

</Project>
33 changes: 32 additions & 1 deletion Community.Blazor.MapLibre/MapLibre.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,40 @@ public async Task FinishGeometryAsync()
}
await _jsModule.InvokeVoidAsync("finishGeometry", MapId);
}

/// <summary>
/// Queries the map for rendered features within a specified geometry or options.
/// </summary>
/// <returns>An array of features.</returns>
public async ValueTask<object[]> GetTerraDrawGeometriesAsync()
{
return await _jsModule.InvokeAsync<object[]>("getTerraDrawGeometries", MapId);
}

[JSInvokable] public Task OnTerraDrawReady() => Task.CompletedTask;
[JSInvokable] public Task OnTerraDrawChanged(string geoJson) => Task.CompletedTask;


public async Task<Listener> AddTerraDrawFinishListener<T>(Action<T> handler)
{
var callback = new CallbackHandler(_jsModule, "finish", handler, typeof(T));
var reference = DotNetObjectReference.Create(callback);
_references.TryAdd(Guid.NewGuid(), reference);

await _jsModule.InvokeVoidAsync("onTerraDrawFinish", MapId, reference);

return new Listener(callback);
}

public async Task<Listener> AddTerraDrawDeleteListener<T>(Action<T> handler)
{
var callback = new CallbackHandler(_jsModule, "delete", handler, typeof(T));
var reference = DotNetObjectReference.Create(callback);
_references.TryAdd(Guid.NewGuid(), reference);

await _jsModule.InvokeVoidAsync("onTerraDrawDelete", MapId, reference);

return new Listener(callback);
}

/// <summary>
/// Adds a geolocate control to the given map container.
Expand Down
41 changes: 38 additions & 3 deletions Community.Blazor.MapLibre/MapLibre.razor.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,6 @@ export function addGeolocateControl(container, options, position) {
export function addNavigationControl(container, options, position) {
const map = mapInstances[container];

console.log("addNavigationControl position: " + position);

if (options === undefined || options === null) {
map.addControl(new maplibregl.NavigationControl(), position || undefined);
} else {
Expand Down Expand Up @@ -187,7 +185,8 @@ export function addTerraDrawTool(container, options) {
return { valid: true }
}
})
drawControls[container] = new terraDraw.TerraDraw({adapter: adapter, modes: [new terraDraw.TerraDrawFreehandMode(), polygonMode, new terraDraw.TerraDrawLineStringMode(), select, new terraDraw.TerraDrawPointMode()]})
const deleteMode = new TerraDrawCoordinateDeleteModeUmd();
drawControls[container] = new terraDraw.TerraDraw({adapter: adapter, modes: [new terraDraw.TerraDrawFreehandMode(), polygonMode, new terraDraw.TerraDrawLineStringMode(), select, new terraDraw.TerraDrawPointMode(), deleteMode]})
}

/**
Expand Down Expand Up @@ -226,6 +225,42 @@ export function finishGeometry(container) {
element.dispatchEvent(event);
}

/**
* Get created geometries from terra-draw.
*
* @param {string} container - The identifier of the map container.
* @returns {Array} geometries - The created geometries.
*/
export function getTerraDrawGeometries(container)
{
const draw = drawControls[container];
return draw.getSnapshot();
}

export function onTerraDrawFinish(container, dotnetReference) {
const draw = drawControls[container];

draw.on("finish", (id, context) => {
if (context.action === "draw" || context.action === "dragCoordinate") {
const features = draw.getSnapshot();
const featuresAsJson = JSON.stringify(features);
dotnetReference.invokeMethodAsync('Invoke', featuresAsJson);
}
});
}

export function onTerraDrawDelete(container, dotnetReference) {
const draw = drawControls[container];

draw.on("change", (ids, type) => {
if (type === "delete") {
const features = draw.getSnapshot();
const featuresAsJson = JSON.stringify(features);
dotnetReference.invokeMethodAsync('Invoke', featuresAsJson);
}
});
}

/**
* Adds a scale control to the given map container.
*
Expand Down
Loading