Skip to content

Commit

Permalink
Added: OnHover shape
Browse files Browse the repository at this point in the history
  • Loading branch information
Raceeend committed Jun 12, 2024
1 parent 9fb822a commit 631a68b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/OpenLayers.Blazor.Demo.Components/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,13 @@ Altitude @(_altitude)m
Mouse Position: @_mousePosition
Visible Extent: @_map?.VisibleExtent
@_info
@_hover_info
</pre>
</p>
</div>
</div>

<SwissMap @ref="_map" OnClick="OnMapClick" OnPointerMove="OnPointerMove" OnRenderComplete="@(() => { _info = "Render complete: " + DateTime.Now.ToString("h:mm:ss.ms"); })" @bind-Zoom="_zoom" Style="height:800px;" Class="card" InteractionsEnabled="@_enableInteractions" @bind-Rotation="_rotation" ZoomSliderControl FullScreenControl>
<SwissMap @ref="_map" OnHover="OnHover" OnClick="OnMapClick" OnPointerMove="OnPointerMove" OnRenderComplete="@(() => { _info = "Render complete: " + DateTime.Now.ToString("h:mm:ss.ms"); })" @bind-Zoom="_zoom" Style="height:800px;" Class="card" InteractionsEnabled="@_enableInteractions" @bind-Rotation="_rotation" ZoomSliderControl FullScreenControl>
<Popup>
<div id="popup" class="ol-box">
@if (context is Marker marker)
Expand All @@ -73,13 +74,14 @@ Visible Extent: @_map?.VisibleExtent

<CodeView Source="Index.razor" />

@code {
@code {
private SwissMap _map = null!;
private Coordinate? _mousePosition;
private double _zoom = 3;
private decimal _altitude;
private Coordinate? _lastPosition;
private string _info = "";
private string _hover_info = "";
bool _enableInteractions = true;
private double _rotation;

Expand All @@ -92,6 +94,12 @@ Visible Extent: @_map?.VisibleExtent
await GetAltitude(coordinate);
}

private async Task OnHover(Shape shape)
{
_hover_info = $"HOVERED: " + shape.Id;
await Task.CompletedTask;
}

private void OnPointerMove(Coordinate coordinate)
{
_mousePosition = coordinate;
Expand Down
12 changes: 12 additions & 0 deletions src/OpenLayers.Blazor/Map.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ public Map()
[Parameter]
public EventCallback<Coordinate> OnPointerMove { get; set; }

/// <summary>
/// Event when the pointer hovers over a shape
/// </summary>
[Parameter]
public EventCallback<Shape> OnHover { get; set; }

/// <summary>
/// Event when the rendering is complete
/// </summary>
Expand Down Expand Up @@ -440,6 +446,12 @@ public Task OnInternalPointerMove(Coordinate coordinate)
return OnPointerMove.InvokeAsync(coordinate);
}

[JSInvokable]
public Task OnInternalHover(Internal.Shape shape)
{
return OnHover.InvokeAsync(new Shape(shape));
}

[JSInvokable]
public async Task OnInternalCenterChanged(Coordinate coordinate)
{
Expand Down
8 changes: 8 additions & 0 deletions src/OpenLayers.Blazor/wwwroot/openlayers_interop.js
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,14 @@ MapOL.prototype.onMapPointerMove = function (evt, element) {
if (evt.dragging || Number.isNaN(evt.coordinate[0])) {
return;
}

var that = this;
this.Map.forEachFeatureAtPixel(evt.pixel,
function (feature) {
const shape = that.mapFeatureToShape(feature);
that.Instance.invokeMethodAsync("OnInternalHover", shape);
});

const coordinate = ol.proj.transform(evt.coordinate,
this.Map.getView().getProjection(),
this.Options.coordinatesProjection);
Expand Down

0 comments on commit 631a68b

Please sign in to comment.