Skip to content

Commit 1f32265

Browse files
author
AlexKven
committed
#29 Allow Zoom and Center to be set with the property on Map.
1 parent 409d19a commit 1f32265

File tree

5 files changed

+128
-14
lines changed

5 files changed

+128
-14
lines changed

BlazorLeaflet/BlazorLeaflet.Samples/Pages/Index.razor

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@page "/"
1+
@page "/"
22
@using System.Drawing
33
@using BlazorLeaflet.Models
44
@inject CityService cityService
@@ -14,6 +14,7 @@
1414
<button class="btn btn-primary mb-2" @onclick="FindCity">Search</button>
1515
<button class="btn btn-primary mb-2" @onclick="ZoomMap">Zoom</button>
1616
<button class="btn btn-primary mb-2" @onclick="PanToNY">Pan to New York</button>
17+
<button class="btn btn-primary mb-2" @onclick="SetToNY">Set center at New York</button>
1718
</div>
1819

1920
<div style="height: 500px; width: 500px;">
@@ -102,12 +103,18 @@
102103

103104
private void ZoomMap()
104105
{
105-
_map.FitBounds(new PointF(45.943f, 24.967f), new PointF(46.943f, 25.967f), maxZoom: 5f);
106-
}
107-
108-
private void PanToNY()
109-
{
110-
_map.PanTo(new PointF(40.713185f, -74.0072333f), animate: true, duration: 10f);
106+
_map.FitBounds(new PointF(45.943f, 24.967f), new PointF(46.943f, 25.967f), maxZoom: 5f);
107+
}
108+
109+
private void PanToNY()
110+
{
111+
_map.PanTo(new PointF(40.713185f, -74.0072333f), animate: true, duration: 10f);
112+
}
113+
114+
private void SetToNY()
115+
{
116+
_map.Center = new LatLng(new PointF(40.713185f, -74.0072333f));
117+
_map.Zoom = 15;
111118
}
112119

113120
}

BlazorLeaflet/BlazorLeaflet/LeafletInterops.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ public static ValueTask<LatLng> GetCenter(IJSRuntime jsRuntime, string mapId) =>
8888
public static ValueTask<float> GetZoom(IJSRuntime jsRuntime, string mapId) =>
8989
jsRuntime.InvokeAsync<float>($"{_BaseObjectContainer}.getZoom", mapId);
9090

91+
public static async Task SetZoom(IJSRuntime jsRuntime, string mapId, float zoomLevel)
92+
{
93+
await jsRuntime.InvokeVoidAsync($"{_BaseObjectContainer}.setZoom", mapId, zoomLevel);
94+
}
95+
9196
public static ValueTask ZoomIn(IJSRuntime jsRuntime, string mapId, MouseEventArgs e) =>
9297
jsRuntime.InvokeVoidAsync($"{_BaseObjectContainer}.zoomIn", mapId, e);
9398

BlazorLeaflet/BlazorLeaflet/Map.cs

Lines changed: 76 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,38 @@ namespace BlazorLeaflet
1616
{
1717
public class Map
1818
{
19+
private LatLng _Center = new LatLng();
1920
/// <summary>
20-
/// Initial geographic center of the map
21+
/// Geographic center of the map
2122
/// </summary>
22-
public LatLng Center { get; set; } = new LatLng();
23+
///
24+
public LatLng Center
25+
{
26+
get => _Center;
27+
set
28+
{
29+
_Center = value;
30+
if (_isInitialized)
31+
RunTaskInBackground(async () => await LeafletInterops.PanTo(
32+
_jsRuntime, Id, value.ToPointF(), false, 0, 0, false));
33+
}
34+
}
2335

36+
private float _Zoom;
2437
/// <summary>
25-
/// Initial map zoom level
38+
/// Map zoom level
2639
/// </summary>
27-
public float Zoom { get; set; }
40+
public float Zoom
41+
{
42+
get => _Zoom;
43+
set
44+
{
45+
_Zoom = value;
46+
if (_isInitialized)
47+
RunTaskInBackground(async () => await LeafletInterops.SetZoom(
48+
_jsRuntime, Id, value));
49+
}
50+
}
2851

2952
/// <summary>
3053
/// Minimum zoom level of the map. If not specified and at least one
@@ -84,6 +107,18 @@ public void RaiseOnInitialized()
84107
OnInitialized?.Invoke();
85108
}
86109

110+
private async void RunTaskInBackground(Func<Task> task)
111+
{
112+
try
113+
{
114+
await task();
115+
}
116+
catch (Exception ex)
117+
{
118+
NotifyBackgroundExceptionOccurred(ex);
119+
}
120+
}
121+
87122
/// <summary>
88123
/// Add a layer to the map.
89124
/// </summary>
@@ -195,6 +230,17 @@ public async Task<float> GetZoom() =>
195230
/// </summary>
196231
public async Task ZoomOut(MouseEventArgs e) => await LeafletInterops.ZoomOut(_jsRuntime, Id, e);
197232

233+
private async Task UpdateZoom()
234+
{
235+
_Zoom = await GetZoom();
236+
}
237+
238+
private async Task UpdateCenter()
239+
{
240+
241+
_Center = await GetCenter();
242+
}
243+
198244
#region events
199245

200246
public delegate void MapEventHandler(object sender, Event e);
@@ -238,11 +284,31 @@ public async Task<float> GetZoom() =>
238284

239285
public event MapEventHandler OnZoomEnd;
240286
[JSInvokable]
241-
public void NotifyZoomEnd(Event e) => OnZoomEnd?.Invoke(this, e);
287+
public async void NotifyZoomEnd(Event e)
288+
{
289+
try
290+
{
291+
await UpdateZoom();
292+
}
293+
finally
294+
{
295+
OnZoomEnd?.Invoke(this, e);
296+
}
297+
}
242298

243299
public event MapEventHandler OnMoveEnd;
244300
[JSInvokable]
245-
public void NotifyMoveEnd(Event e) => OnMoveEnd?.Invoke(this, e);
301+
public async void NotifyMoveEnd(Event e)
302+
{
303+
try
304+
{
305+
await UpdateCenter();
306+
}
307+
finally
308+
{
309+
OnMoveEnd?.Invoke(this, e);
310+
}
311+
}
246312

247313
public event MouseEventHandler OnMouseMove;
248314
[JSInvokable]
@@ -264,6 +330,10 @@ public async Task<float> GetZoom() =>
264330
[JSInvokable]
265331
public void NotifyPreClick(MouseEvent eventArgs) => OnPreClick?.Invoke(this, eventArgs);
266332

333+
public event EventHandler<Exception> BackgroundExceptionOccurred;
334+
private void NotifyBackgroundExceptionOccurred(Exception exception) =>
335+
BackgroundExceptionOccurred?.Invoke(this, exception);
336+
267337
#endregion events
268338

269339
#region InteractiveLayerEvents
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Runtime.Serialization;
4+
using System.Text;
5+
6+
namespace BlazorLeaflet.Models
7+
{
8+
public class Bounds
9+
{
10+
[DataMember(Name = "_northEast")]
11+
public LatLng NorthEast { get; set; }
12+
13+
[DataMember(Name = "_southWest")]
14+
public LatLng SouthWest { get; set; }
15+
16+
public Bounds() { }
17+
public Bounds(LatLng southWest, LatLng northEast)
18+
{
19+
NorthEast = northEast;
20+
SouthWest = southWest;
21+
}
22+
23+
public override string ToString() =>
24+
$"NE: {NorthEast.Lat} N, {NorthEast.Lng} E; SW: {SouthWest.Lat} N, {SouthWest.Lng} E";
25+
}
26+
}

BlazorLeaflet/BlazorLeaflet/wwwroot/leafletBlazorInterops.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
maps = {};
1+
maps = {};
22
layers = {};
33

44
window.leafletBlazor = {
@@ -221,6 +221,12 @@ window.leafletBlazor = {
221221
if (map.getZoom() > map.getMinZoom()) {
222222
map.zoomOut(map.options.zoomDelta * (e.shiftKey ? 3 : 1));
223223
}
224+
},
225+
getBounds: function (mapId) {
226+
return maps[mapId].getBounds();
227+
},
228+
setZoom: function (mapId, zoomLevel) {
229+
maps[mapId].setZoom(zoomLevel);
224230
}
225231
};
226232

0 commit comments

Comments
 (0)