From ff59481e328d07392c8e7d42f2bead3ca3b91776 Mon Sep 17 00:00:00 2001 From: benjamint Date: Tue, 31 Oct 2023 12:57:03 +1300 Subject: [PATCH] Fixed multiple geojson renders --- BlazorLeafletInterop/Components/Layers/Misc/GeoJson.cs | 4 +++- ExampleApp/Pages/GeoJsonTest.razor | 8 +++++++- ExampleApp/Properties/launchSettings.json | 2 +- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/BlazorLeafletInterop/Components/Layers/Misc/GeoJson.cs b/BlazorLeafletInterop/Components/Layers/Misc/GeoJson.cs index bd83993..031db57 100644 --- a/BlazorLeafletInterop/Components/Layers/Misc/GeoJson.cs +++ b/BlazorLeafletInterop/Components/Layers/Misc/GeoJson.cs @@ -67,8 +67,9 @@ protected override async Task OnInitializedAsync() await AddTo(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(); @@ -115,6 +116,7 @@ public async Task EachLayer(Action callback) public override async ValueTask DisposeAsync() { if (JsObjectReference is null || Map is null) return; + await ClearLayers(); await RemoveFrom(Map.MapRef, JsObjectReference); if (JsObjectReference != null) await JsObjectReference.DisposeAsync(); DotNetRef?.Dispose(); diff --git a/ExampleApp/Pages/GeoJsonTest.razor b/ExampleApp/Pages/GeoJsonTest.razor index 7970f0f..9024f00 100644 --- a/ExampleApp/Pages/GeoJsonTest.razor +++ b/ExampleApp/Pages/GeoJsonTest.razor @@ -17,6 +17,7 @@

Blazor Leaflet Interop GeoJson Test

+
@code { @@ -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 { { "id", $"{i}" } }; @@ -60,5 +61,10 @@ base.OnInitialized(); CreateRandomMarkersForFeatureCollection(); } + + private void GenerateNewFeatureCollection() + { + CreateRandomMarkersForFeatureCollection(); + } } \ No newline at end of file diff --git a/ExampleApp/Properties/launchSettings.json b/ExampleApp/Properties/launchSettings.json index f8231cd..e52c691 100644 --- a/ExampleApp/Properties/launchSettings.json +++ b/ExampleApp/Properties/launchSettings.json @@ -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" }