BlazorLeaflet
BlazorLeaflet copied to clipboard
Enable getting and setting Center and Zoom properties after initialization
The current Center
and Zoom
properties don't have any effect after the control is initialized. I am working on a project where I need this feature, and I was able to change Map
to have the Center
and Zoom
properties stay updated to the status of the map. If this is something you would want merged into this repository, then assign this issue to me and I can put my code into a proper pull request.
I was able to change Map to have the Center and Zoom properties stay updated to the status of the map.
Are changes to the properties reflected immediately? We might want to make some await UpdateMapAsync()
method to avoid property setters from having too much performance impact.
I think it updates immediately if it is in Blazor client (the async
bit is in case it's running on the server, and the JS needs to be sent over the SignalR connection).
Currently what I have is this:
public LatLng Center
{
get => _Center;
set
{
_Center = value;
if (_isInitialized)
RunTaskInBackground(async () => await LeafletInterops.PanTo(
_jsRuntime, Id, value.ToPointF(), false, 0, 0, false));
}
}
...and RunTaskInBackground
is this:
private async void RunTaskInBackground(Func<Task> task)
{
try
{
await task();
}
catch (Exception) { }
}
I don't think performance is an issue. I may want to add an actual exception handler for this (maybe the map could allow the user to provide a custom exception handler).
As for getting the properties, no async
method is necessary because I subscribe to changes in the Leaflet map. When the property changes, I update the backing field of the Center
and Zoom
properties, so they are already updated to the map's actual center and zoom (same with the Bounds
property in the other issue).
(This is now PR #37).