Skip to content

Commit

Permalink
#30 Add bounds property to the map.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexKven committed Jun 10, 2020
1 parent f6442ae commit a78552d
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 5 deletions.
4 changes: 3 additions & 1 deletion BlazorLeaflet/BlazorLeaflet.Samples/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<div style="height: 500px; width: 500px;">
<LeafletMap Map="_map" />
</div>
<p>Map bounds: @_map.Bounds?.ToString()</p>

@code
{
Expand Down Expand Up @@ -66,7 +67,8 @@
Radius = 10f
};
_map.AddLayer(_circle);
};
};
_map.OnBoundsChanged += (s, e) => this.StateHasChanged();
}

private LatLng _startAt = new LatLng(47.5574007f, 16.3918687f);
Expand Down
17 changes: 16 additions & 1 deletion BlazorLeaflet/BlazorLeaflet/LeafletInterops.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,21 @@ public static ValueTask<LatLng> GetCenter(IJSRuntime jsRuntime, string mapId) =>
jsRuntime.InvokeAsync<LatLng>($"{_BaseObjectContainer}.getCenter", mapId);

public static ValueTask<float> GetZoom(IJSRuntime jsRuntime, string mapId) =>
jsRuntime.InvokeAsync<float>($"{_BaseObjectContainer}.getZoom", mapId);
jsRuntime.InvokeAsync<float>($"{_BaseObjectContainer}.getZoom", mapId);

// Private class only for deserialization from JSON (since the JSON names on the bounds are "_southWest"
// with the _). Since deserialization in JSRuntime is non-customizable, this is a good solution for now.
private class _Bounds
{
public LatLng _southWest { get; set; }
public LatLng _northEast { get; set; }

public Bounds AsBounds() => new Bounds(_southWest, _northEast);
}

public static async Task<Bounds> GetBounds(IJSRuntime jsRuntime, string mapId)
{
return (await jsRuntime.InvokeAsync<_Bounds>($"{_BaseObjectContainer}.getBounds", mapId)).AsBounds();
}
}
}
65 changes: 63 additions & 2 deletions BlazorLeaflet/BlazorLeaflet/Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ public class Map
/// </summary>
public float Zoom { get; set; }


/// <summary>
/// Map bounds
/// </summary>
public Bounds Bounds { get; private set; }

/// <summary>
/// Minimum zoom level of the map. If not specified and at least one
/// GridLayer or TileLayer is in the map, the lowest of their minZoom
Expand Down Expand Up @@ -74,6 +80,19 @@ public void RaiseOnInitialized()
{
_isInitialized = true;
OnInitialized?.Invoke();
RunTaskInBackground(UpdateBounds);
}

private async void RunTaskInBackground(Func<Task> task)
{
try
{
await task();
}
catch (Exception ex)
{
NotifyBackgroundExceptionOccurred(ex);
}
}

/// <summary>
Expand Down Expand Up @@ -171,6 +190,14 @@ public void PanTo(PointF position, bool animate = false, float duration = 0.25f,

public async Task<LatLng> GetCenter() => await LeafletInterops.GetCenter(_jsRuntime, Id);
public async Task<float> GetZoom() => await LeafletInterops.GetZoom(_jsRuntime, Id);
public async Task<Bounds> GetBounds() => await LeafletInterops.GetBounds(_jsRuntime, Id);


private async Task UpdateBounds()
{
Bounds = await GetBounds();
OnBoundsChanged?.Invoke(this, new EventArgs());
}

#region events

Expand Down Expand Up @@ -215,11 +242,41 @@ public void PanTo(PointF position, bool animate = false, float duration = 0.25f,

public event MapEventHandler OnZoomEnd;
[JSInvokable]
public void NotifyZoomEnd(Event e) => OnZoomEnd?.Invoke(this, e);
public async void NotifyZoomEnd(Event e)
{
try
{
await UpdateBounds();
}
catch (Exception ex)
{
NotifyBackgroundExceptionOccurred(ex);
}
finally
{
OnZoomEnd?.Invoke(this, e);
}
}

public event MapEventHandler OnMoveEnd;
[JSInvokable]
public void NotifyMoveEnd(Event e) => OnMoveEnd?.Invoke(this, e);
public async void NotifyMoveEnd(Event e)
{
try
{
await UpdateBounds();
}
catch (Exception ex)
{
NotifyBackgroundExceptionOccurred(ex);
}
finally
{
OnMoveEnd?.Invoke(this, e);
}
}

public event EventHandler OnBoundsChanged;

public event MouseEventHandler OnMouseMove;
[JSInvokable]
Expand All @@ -241,6 +298,10 @@ public void PanTo(PointF position, bool animate = false, float duration = 0.25f,
[JSInvokable]
public void NotifyPreClick(MouseEvent eventArgs) => OnPreClick?.Invoke(this, eventArgs);

public event EventHandler<Exception> BackgroundExceptionOccurred;
private void NotifyBackgroundExceptionOccurred(Exception exception) =>
BackgroundExceptionOccurred?.Invoke(this, exception);

#endregion events

#region InteractiveLayerEvents
Expand Down
26 changes: 26 additions & 0 deletions BlazorLeaflet/BlazorLeaflet/Models/Bounds.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Text;

namespace BlazorLeaflet.Models
{
public class Bounds
{
[DataMember(Name = "_northEast")]
public LatLng NorthEast { get; set; }

[DataMember(Name = "_southWest")]
public LatLng SouthWest { get; set; }

public Bounds() { }
public Bounds(LatLng southWest, LatLng northEast)
{
NorthEast = northEast;
SouthWest = southWest;
}

public override string ToString() =>
$"NE: {NorthEast.Lat} N, {NorthEast.Lng} E; SW: {SouthWest.Lat} N, {SouthWest.Lng} E";
}
}
5 changes: 4 additions & 1 deletion BlazorLeaflet/BlazorLeaflet/wwwroot/leafletBlazorInterops.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
maps = {};
maps = {};
layers = {};

window.leafletBlazor = {
Expand Down Expand Up @@ -206,6 +206,9 @@ window.leafletBlazor = {
},
getZoom: function (mapId) {
return maps[mapId].getZoom();
},
getBounds: function (mapId) {
return maps[mapId].getBounds();
}
};

Expand Down

0 comments on commit a78552d

Please sign in to comment.