Skip to content

Commit

Permalink
Merge pull request #296 from cruiserkernan/feature/remove_controls
Browse files Browse the repository at this point in the history
Add Custom Control Removal Functionality
  • Loading branch information
valentasm1 authored Dec 5, 2023
2 parents 05bef8a + 155535e commit f022c9a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
10 changes: 10 additions & 0 deletions GoogleMapsComponents/Maps/Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ public async Task AddControl(ControlPosition position, ElementReference referenc
await _jsObjectRef.JSRuntime.MyInvokeAsync<object>("blazorGoogleMaps.objectManager.addControls", this.Guid.ToString(), position, reference);
}

public async Task RemoveControl(ControlPosition position, ElementReference reference)
{
await _jsObjectRef.JSRuntime.MyInvokeAsync<object>("blazorGoogleMaps.objectManager.removeControl", this.Guid.ToString(), position, reference);
}

public async Task RemoveAllControls(ControlPosition position)
{
await _jsObjectRef.JSRuntime.MyInvokeAsync<object>("blazorGoogleMaps.objectManager.clearControls", this.Guid.ToString(), position);
}

public async Task AddImageLayer(ImageMapType reference)
{
await _jsObjectRef.JSRuntime.MyInvokeAsync<object>("blazorGoogleMaps.objectManager.addImageLayer", this.Guid.ToString(), reference.Guid.ToString());
Expand Down
22 changes: 21 additions & 1 deletion GoogleMapsComponents/wwwroot/js/objectManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,10 +392,30 @@
addControls(args) {
let map = mapObjects[args[0]];
let elem = args[2];
let clone = elem.cloneNode(true);
//I know i am lazy. Two quotes appear after serialization
let position = getGooglePositionFromString(args[1].replace("\"", "").replace("\"", ""));

map.controls[position].push(elem);
map.controls[position].push(clone);
},
removeControl(args) {
let map = mapObjects[args[0]];
let elem = args[2];
let position = getGooglePositionFromString(args[1].replace("\"", "").replace("\"", ""));

var arr = map.controls[position].getArray();
for (var i = 0, len = arr.length; i < len; i++) {
if (arr[i].id === elem.id) {
map.controls[position].removeAt(i);
return;
}
}
},
clearControls(args) {
let map = mapObjects[args[0]];
let position = getGooglePositionFromString(args[1].replace("\"", "").replace("\"", ""));

map.controls[position].clear();
},
addImageLayer(args) {
let map = mapObjects[args[0]];
Expand Down
3 changes: 3 additions & 0 deletions ServerSideDemo/Pages/MapLegendPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
<h1>Google Map Legend</h1>

<GoogleMap @ref="@map1" Id="map1" Options="@mapOptions" OnAfterInit="@AfterMapInit"></GoogleMap>
<button @onclick="AddLegend">Add Legend</button>
<button @onclick="RemoveLegend">Remove Legend</button>
<button @onclick="RemoveAllControls">Remove All Controls</button>

<div id="legend" @ref="@legendReference" style="visibility: hidden"><h3>Legend</h3></div>

Expand Down
15 changes: 15 additions & 0 deletions ServerSideDemo/Pages/MapLegendPage.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
}

private async Task AfterMapInit()
{

}

private async Task RemoveLegend()
{
await map1.InteropObject.RemoveControl(ControlPosition.TopLeft, legendReference);
}

private async Task RemoveAllControls()
{
await map1.InteropObject.RemoveAllControls(ControlPosition.TopLeft);
}

private async Task AddLegend()
{
await map1.InteropObject.AddControl(ControlPosition.TopLeft, legendReference);
}
Expand Down

0 comments on commit f022c9a

Please sign in to comment.