From 4170390e18b805a84214371546f92067cc55318f Mon Sep 17 00:00:00 2001 From: Pierre Schroeder Date: Sat, 9 Jan 2021 22:08:27 +0100 Subject: [PATCH 1/3] Add test-code for Marker.SetLabel() to MapMarkers example --- ClientSideDemoNet5/Pages/MapMarker.razor | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/ClientSideDemoNet5/Pages/MapMarker.razor b/ClientSideDemoNet5/Pages/MapMarker.razor index f768d865..12c3037b 100644 --- a/ClientSideDemoNet5/Pages/MapMarker.razor +++ b/ClientSideDemoNet5/Pages/MapMarker.razor @@ -9,6 +9,8 @@ + + @@ -23,6 +25,9 @@ private MapEventList eventList = default!; + private string labelText = ""; + + protected override void OnInitialized() { mapOptions = new MapOptions() @@ -78,6 +83,7 @@ await marker.AddListener("click", async e => { var markerLabel = await marker.GetLabel(); + labelText = markerLabel; _events.Add("click on " + markerLabel); StateHasChanged(); @@ -108,4 +114,15 @@ await lastMarker.SetPosition(center); } + private async Task UpdateLabelText() + { + if (!markers.Any()) + { + return; + } + + var lastMarker = markers.Peek(); + await lastMarker.SetLabel(labelText); + } + } \ No newline at end of file From 83976f7731c8cc6566bda9933f5544f32d9780ce Mon Sep 17 00:00:00 2001 From: Pierre Schroeder Date: Sat, 9 Jan 2021 22:08:41 +0100 Subject: [PATCH 2/3] change Marker.SetLabel() input type from Symbol to string --- GoogleMapsComponents/Maps/Marker.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GoogleMapsComponents/Maps/Marker.cs b/GoogleMapsComponents/Maps/Marker.cs index 051ac0bb..a1473cc6 100644 --- a/GoogleMapsComponents/Maps/Marker.cs +++ b/GoogleMapsComponents/Maps/Marker.cs @@ -139,7 +139,7 @@ public Task SetIcon(Icon icon) icon); } - public Task SetLabel(Symbol label) + public Task SetLabel(string label) { return _jsObjectRef.InvokeAsync( "setLabel", From f3ce4ecc0ea9c0c4a3d5d34324ba2d28ef75dfcb Mon Sep 17 00:00:00 2001 From: Pierre Schroeder Date: Mon, 11 Jan 2021 00:19:06 +0100 Subject: [PATCH 3/3] let label-property handle string and MarkerLabel alike --- ClientSideDemoNet5/Pages/MapMarker.razor | 67 +++++++++++++++++------- GoogleMapsComponents/Maps/Marker.cs | 33 ++++++++++-- 2 files changed, 79 insertions(+), 21 deletions(-) diff --git a/ClientSideDemoNet5/Pages/MapMarker.razor b/ClientSideDemoNet5/Pages/MapMarker.razor index 12c3037b..e2464526 100644 --- a/ClientSideDemoNet5/Pages/MapMarker.razor +++ b/ClientSideDemoNet5/Pages/MapMarker.razor @@ -9,8 +9,8 @@ - - + + @@ -27,7 +27,6 @@ private string labelText = ""; - protected override void OnInitialized() { mapOptions = new MapOptions() @@ -44,17 +43,29 @@ private async Task AddMarker() { - var marker = await Marker.CreateAsync(map1.JsRuntime, new MarkerOptions() - { - Position = await map1.InteropObject.GetCenter(), - Map = map1.InteropObject, - Label = $"Test {markers.Count()}", - Icon = new Icon() + var marker = await Marker.CreateAsync(map1.JsRuntime, markers.Count() % 2 == 0 ? + new MarkerOptions() { - Url = "https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png" - } - //Icon = "https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png" - }); + Position = await map1.InteropObject.GetCenter(), + Map = map1.InteropObject, + Label = new MarkerLabel { Text = $"Test {markers.Count()}", FontWeight = "bold"}, + Icon = new Icon() + { + Url = "https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png" + } + //Icon = "https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png" + } : + new MarkerOptions() + { + Position = await map1.InteropObject.GetCenter(), + Map = map1.InteropObject, + Label = $"Test {markers.Count()}", + Icon = new Icon() + { + Url = "https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png" + } + //Icon = "https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png" + }); //await marker.SetMap(map1); @@ -79,12 +90,12 @@ //} markers.Push(marker); + labelText = await marker.GetLabelText(); await marker.AddListener("click", async e => { - var markerLabel = await marker.GetLabel(); - labelText = markerLabel; - _events.Add("click on " + markerLabel); + string markerLabelText = await marker.GetLabelText(); + _events.Add("click on " + markerLabelText); StateHasChanged(); await e.Stop(); @@ -100,6 +111,7 @@ var lastMarker = markers.Pop(); await lastMarker.SetMap(null); + labelText = markers.Any() ? await markers.Peek().GetLabelText() : ""; } private async Task Recenter() @@ -114,7 +126,24 @@ await lastMarker.SetPosition(center); } - private async Task UpdateLabelText() + private async Task UpdateLabelText(ChangeEventArgs e) + { + if (!markers.Any()) + { + return; + } + + string newLabelText = e.Value?.ToString() ?? "" ; + if (labelText != newLabelText) + { + labelText = newLabelText; + + var lastMarker = markers.Peek(); + await lastMarker.SetLabelText(labelText); + } + } + + private async Task ToggleLabelFontWeight() { if (!markers.Any()) { @@ -122,7 +151,9 @@ } var lastMarker = markers.Peek(); - await lastMarker.SetLabel(labelText); + var lastMarkerLabel = await lastMarker.GetLabelMarkerLabel(); + lastMarkerLabel.FontWeight = (lastMarkerLabel.FontWeight == "bold") ? "normal" : "bold"; + await lastMarker.SetLabel(lastMarkerLabel); } } \ No newline at end of file diff --git a/GoogleMapsComponents/Maps/Marker.cs b/GoogleMapsComponents/Maps/Marker.cs index a1473cc6..19f27e9f 100644 --- a/GoogleMapsComponents/Maps/Marker.cs +++ b/GoogleMapsComponents/Maps/Marker.cs @@ -55,9 +55,23 @@ public async Task> GetIcon() return result; } - public Task GetLabel() + public Task> GetLabel() { - return _jsObjectRef.InvokeAsync("getLabel"); + return _jsObjectRef.InvokeAsync("getLabel"); + } + + public async Task GetLabelText() + { + OneOf markerLabel = await GetLabel(); + return markerLabel.IsT0 ? markerLabel.AsT0 : markerLabel.AsT1.Text; + } + + public async Task GetLabelMarkerLabel() + { + OneOf markerLabel = await GetLabel(); + return markerLabel.IsT1 ? + markerLabel.AsT1 : + new MarkerLabel { Text = markerLabel.AsT0 }; } public Task GetPosition() @@ -139,13 +153,26 @@ public Task SetIcon(Icon icon) icon); } - public Task SetLabel(string label) + public Task SetLabel(OneOf label) { return _jsObjectRef.InvokeAsync( "setLabel", label); } + public async Task SetLabelText(string labelText) + { + OneOf markerLabel = await GetLabel(); + if (markerLabel.IsT1) + { + MarkerLabel label = markerLabel.AsT1; + label.Text = labelText; + await SetLabel(label); + } + else + await SetLabel(labelText); + } + public Task SetOpacity(float opacity) { return _jsObjectRef.InvokeAsync(