Skip to content

Commit 496cab6

Browse files
author
AlexKven
committed
#29 Allow Zoom and Center to be set with the property on Map.
1 parent f6442ae commit 496cab6

File tree

5 files changed

+129
-15
lines changed

5 files changed

+129
-15
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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using BlazorLeaflet.Models;
1+
using BlazorLeaflet.Models;
22
using Microsoft.JSInterop;
33
using System;
44
using System.Drawing;
@@ -61,5 +61,10 @@ public static ValueTask<LatLng> GetCenter(IJSRuntime jsRuntime, string mapId) =>
6161

6262
public static ValueTask<float> GetZoom(IJSRuntime jsRuntime, string mapId) =>
6363
jsRuntime.InvokeAsync<float>($"{_BaseObjectContainer}.getZoom", mapId);
64+
65+
public static async Task SetZoom(IJSRuntime jsRuntime, string mapId, float zoomLevel)
66+
{
67+
await jsRuntime.InvokeVoidAsync($"{_BaseObjectContainer}.setZoom", mapId, zoomLevel);
68+
}
6469
}
6570
}

BlazorLeaflet/BlazorLeaflet/Map.cs

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

35+
private float _Zoom;
2336
/// <summary>
24-
/// Initial map zoom level
37+
/// Map zoom level
2538
/// </summary>
26-
public float Zoom { get; set; }
39+
public float Zoom
40+
{
41+
get => _Zoom;
42+
set
43+
{
44+
_Zoom = value;
45+
if (_isInitialized)
46+
RunTaskInBackground(async () => await LeafletInterops.SetZoom(
47+
_jsRuntime, Id, value));
48+
}
49+
}
2750

2851
/// <summary>
2952
/// Minimum zoom level of the map. If not specified and at least one
@@ -76,6 +99,18 @@ public void RaiseOnInitialized()
7699
OnInitialized?.Invoke();
77100
}
78101

102+
private async void RunTaskInBackground(Func<Task> task)
103+
{
104+
try
105+
{
106+
await task();
107+
}
108+
catch (Exception ex)
109+
{
110+
NotifyBackgroundExceptionOccurred(ex);
111+
}
112+
}
113+
79114
/// <summary>
80115
/// Add a layer to the map.
81116
/// </summary>
@@ -172,6 +207,17 @@ public void PanTo(PointF position, bool animate = false, float duration = 0.25f,
172207
public async Task<LatLng> GetCenter() => await LeafletInterops.GetCenter(_jsRuntime, Id);
173208
public async Task<float> GetZoom() => await LeafletInterops.GetZoom(_jsRuntime, Id);
174209

210+
private async Task UpdateZoom()
211+
{
212+
_Zoom = await GetZoom();
213+
}
214+
215+
private async Task UpdateCenter()
216+
{
217+
218+
_Center = await GetCenter();
219+
}
220+
175221
#region events
176222

177223
public delegate void MapEventHandler(object sender, Event e);
@@ -215,11 +261,31 @@ public void PanTo(PointF position, bool animate = false, float duration = 0.25f,
215261

216262
public event MapEventHandler OnZoomEnd;
217263
[JSInvokable]
218-
public void NotifyZoomEnd(Event e) => OnZoomEnd?.Invoke(this, e);
264+
public async void NotifyZoomEnd(Event e)
265+
{
266+
try
267+
{
268+
await UpdateZoom();
269+
}
270+
finally
271+
{
272+
OnZoomEnd?.Invoke(this, e);
273+
}
274+
}
219275

220276
public event MapEventHandler OnMoveEnd;
221277
[JSInvokable]
222-
public void NotifyMoveEnd(Event e) => OnMoveEnd?.Invoke(this, e);
278+
public async void NotifyMoveEnd(Event e)
279+
{
280+
try
281+
{
282+
await UpdateCenter();
283+
}
284+
finally
285+
{
286+
OnMoveEnd?.Invoke(this, e);
287+
}
288+
}
223289

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

310+
public event EventHandler<Exception> BackgroundExceptionOccurred;
311+
private void NotifyBackgroundExceptionOccurred(Exception exception) =>
312+
BackgroundExceptionOccurred?.Invoke(this, exception);
313+
244314
#endregion events
245315

246316
#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 = {
@@ -206,6 +206,12 @@ window.leafletBlazor = {
206206
},
207207
getZoom: function (mapId) {
208208
return maps[mapId].getZoom();
209+
},
210+
getBounds: function (mapId) {
211+
return maps[mapId].getBounds();
212+
},
213+
setZoom: function (mapId, zoomLevel) {
214+
maps[mapId].setZoom(zoomLevel);
209215
}
210216
};
211217

0 commit comments

Comments
 (0)