Skip to content

Commit

Permalink
Merge pull request #25
Browse files Browse the repository at this point in the history
Fix: Multiple GeoJson Renders when data changes
  • Loading branch information
bjtrounson authored Oct 30, 2023
2 parents a58f5ee + ff59481 commit f9ac9f4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
4 changes: 3 additions & 1 deletion BlazorLeafletInterop/Components/Layers/Misc/GeoJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ protected override async Task OnInitializedAsync()
await AddTo<GeoJson>(Map.MapRef, JsObjectReference);
}

protected override async Task OnParametersSetAsync()
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender) return;
if (JsObjectReference is null) return;
if (EachLayerCallbackSet) { EachLayerCallbackSet = false; return; }
await ClearLayers();
Expand Down Expand Up @@ -115,6 +116,7 @@ public async Task EachLayer(Action<IJSObjectReference, Feature?> callback)
public override async ValueTask DisposeAsync()
{
if (JsObjectReference is null || Map is null) return;
await ClearLayers();
await RemoveFrom<GeoJson>(Map.MapRef, JsObjectReference);
if (JsObjectReference != null) await JsObjectReference.DisposeAsync();
DotNetRef?.Dispose();
Expand Down
8 changes: 7 additions & 1 deletion ExampleApp/Pages/GeoJsonTest.razor
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<div style="position: absolute; top: 5%; left: 5%; z-index: 10000; background-color: aquamarine; padding: 10px; border-radius: 10px;">
<h3>Blazor Leaflet Interop GeoJson Test</h3>
<button @onclick="@(() => GeoJsonLayer?.EachLayer(_onEachLayer))">Open Popup</button>
<button @onclick=GenerateNewFeatureCollection>Generate More Markers</button>
</div>

@code {
Expand All @@ -35,7 +36,7 @@
private void CreateRandomMarkersForFeatureCollection()
{
var random = new Random();
for (var i = 0; i < 1000; i++)
for (var i = 0; i < 100; i++)
{
var point = new Point(new Position(random.Next(0, 100), random.Next(0, 100)));
var properties = new Dictionary<string, object> { { "id", $"{i}" } };
Expand All @@ -60,5 +61,10 @@
base.OnInitialized();
CreateRandomMarkersForFeatureCollection();
}

private void GenerateNewFeatureCollection()
{
CreateRandomMarkersForFeatureCollection();
}

}
2 changes: 1 addition & 1 deletion ExampleApp/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"dotnetRunMessages": true,
"launchBrowser": true,
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
"applicationUrl": "http://localhost:5026",
"applicationUrl": "http://0.0.0.0:5026",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
Expand Down

0 comments on commit f9ac9f4

Please sign in to comment.