Skip to content

Commit

Permalink
Merge pull request #87 from vamPierchen/bug/fix_type_Marker_SetLabel
Browse files Browse the repository at this point in the history
Fix type for Marker::SetLabel()
  • Loading branch information
valentasm1 authored Jan 13, 2021
2 parents 3cd1a99 + f3ce4ec commit 7664d6e
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 15 deletions.
72 changes: 60 additions & 12 deletions ClientSideDemoNet5/Pages/MapMarker.razor
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<button @onclick="AddMarker">Add marker</button>
<button @onclick="RemoveMarker">Remove marker</button>
<button @onclick="Recenter">Re-center marker</button>
<input type="text" value="@labelText" @onchange="UpdateLabelText" />
<button @onclick="ToggleLabelFontWeight">Toggle label font-weight</button>

<MapEventList @ref="@eventList" Events="@_events"></MapEventList>

Expand All @@ -23,6 +25,8 @@

private MapEventList eventList = default!;

private string labelText = "";

protected override void OnInitialized()
{
mapOptions = new MapOptions()
Expand All @@ -39,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);
Expand All @@ -74,11 +90,12 @@
//}
markers.Push(marker);
labelText = await marker.GetLabelText();

await marker.AddListener<MouseEvent>("click", async e =>
{
var markerLabel = await marker.GetLabel();
_events.Add("click on " + markerLabel);
string markerLabelText = await marker.GetLabelText();
_events.Add("click on " + markerLabelText);
StateHasChanged();

await e.Stop();
Expand All @@ -94,6 +111,7 @@

var lastMarker = markers.Pop();
await lastMarker.SetMap(null);
labelText = markers.Any() ? await markers.Peek().GetLabelText() : "";
}

private async Task Recenter()
Expand All @@ -108,4 +126,34 @@
await lastMarker.SetPosition(center);
}

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())
{
return;
}

var lastMarker = markers.Peek();
var lastMarkerLabel = await lastMarker.GetLabelMarkerLabel();
lastMarkerLabel.FontWeight = (lastMarkerLabel.FontWeight == "bold") ? "normal" : "bold";
await lastMarker.SetLabel(lastMarkerLabel);
}

}
33 changes: 30 additions & 3 deletions GoogleMapsComponents/Maps/Marker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,23 @@ public async Task<OneOf<string, Icon, Symbol>> GetIcon()
return result;
}

public Task<string> GetLabel()
public Task<OneOf<string, MarkerLabel>> GetLabel()
{
return _jsObjectRef.InvokeAsync<string>("getLabel");
return _jsObjectRef.InvokeAsync<string, MarkerLabel>("getLabel");
}

public async Task<string> GetLabelText()
{
OneOf<string, MarkerLabel> markerLabel = await GetLabel();
return markerLabel.IsT0 ? markerLabel.AsT0 : markerLabel.AsT1.Text;
}

public async Task<MarkerLabel> GetLabelMarkerLabel()
{
OneOf<string, MarkerLabel> markerLabel = await GetLabel();
return markerLabel.IsT1 ?
markerLabel.AsT1 :
new MarkerLabel { Text = markerLabel.AsT0 };
}

public Task<LatLngLiteral> GetPosition()
Expand Down Expand Up @@ -139,13 +153,26 @@ public Task SetIcon(Icon icon)
icon);
}

public Task SetLabel(Symbol label)
public Task SetLabel(OneOf<string, MarkerLabel> label)
{
return _jsObjectRef.InvokeAsync(
"setLabel",
label);
}

public async Task SetLabelText(string labelText)
{
OneOf<string, MarkerLabel> 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(
Expand Down

0 comments on commit 7664d6e

Please sign in to comment.