Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added: OnHover shape #61

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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