From 1441b9030a2439ab7d185a7bbc17431995b9244c Mon Sep 17 00:00:00 2001 From: Arnaud Leclerc Date: Thu, 19 Nov 2020 21:57:55 +0100 Subject: [PATCH] Adding SetCameraOptions on Map (#10) * Adding SetCameraOptions to the map * Adding SetCameraOptions to the map +semver: minor --- .../Pages/HtmlMarkersRemove.razor | 2 + .../Constants/Constants.cs | 1 + .../Map/AzureMap.razor | 43 +++++++++++-------- .../Map/CameraOptions.cs | 2 +- src/AzureMapsControl.Components/Map/Map.cs | 13 +++++- .../wwwroot/azure-maps-control.js | 15 ++++--- .../Map/Map.cs | 17 ++++++++ 7 files changed, 66 insertions(+), 27 deletions(-) diff --git a/samples/AzureMapsControl.Sample/Pages/HtmlMarkersRemove.razor b/samples/AzureMapsControl.Sample/Pages/HtmlMarkersRemove.razor index e04c575..d260b43 100644 --- a/samples/AzureMapsControl.Sample/Pages/HtmlMarkersRemove.razor +++ b/samples/AzureMapsControl.Sample/Pages/HtmlMarkersRemove.razor @@ -27,5 +27,7 @@ ( _marker ); + + await args.Map.SetCameraOptionsAsync(options => options.Center = args.Position); } } \ No newline at end of file diff --git a/src/AzureMapsControl.Components/Constants/Constants.cs b/src/AzureMapsControl.Components/Constants/Constants.cs index c692208..5afeb5d 100644 --- a/src/AzureMapsControl.Components/Constants/Constants.cs +++ b/src/AzureMapsControl.Components/Constants/Constants.cs @@ -13,6 +13,7 @@ internal static class JsConstants internal const string MethodAddMap = "addMap"; internal const string MethodClearMap = "clearMap"; internal const string MethodSetOptions = "setOptions"; + internal const string MethodSetCameraOptions = "setCameraOptions"; internal const string MethodAddControl = "addControls"; diff --git a/src/AzureMapsControl.Components/Map/AzureMap.razor b/src/AzureMapsControl.Components/Map/AzureMap.razor index e8511b2..d3ca387 100644 --- a/src/AzureMapsControl.Components/Map/AzureMap.razor +++ b/src/AzureMapsControl.Components/Map/AzureMap.razor @@ -888,23 +888,24 @@ { if (mapEvent.Type == "ready") { + var cameraOptions = new CameraOptions + { + Bearing = (int?)Bearing, + CenterOffset = CenterOffset, + Duration = Duration, + MaxZoom = MaxZoom, + MinZoom = MinZoom, + Pitch = Pitch, + Type = CameryType?.ToString(), + Bounds = Bounds, + MaxBounds = Bounds != null ? MaxBounds : null, + Offset = Bounds != null ? Offset : null, + Padding = Bounds != null ? Padding : null, + Center = Bounds == null ? Center : null, + Zoom = Bounds == null ? Zoom : null + }; await JSRuntime.InvokeVoidAsync(Constants.JsConstants.MethodSetOptions.ToAzureMapsControlNamespace(), - new CameraOptions - { - Bearing = (int?)Bearing, - CenterOffset = CenterOffset, - Duration = Duration, - MaxZoom = MaxZoom, - MinZoom = MinZoom, - Pitch = Pitch, - Type = CameryType?.ToString(), - Bounds = Bounds, - MaxBounds = Bounds != null ? MaxBounds : null, - Offset = Bounds != null ? Offset : null, - Padding = Bounds != null ? Padding : null, - Center = Bounds == null ? Center : null, - Zoom = Bounds == null ? Zoom : null - }, + cameraOptions, new StyleOptions { AutoResize = AutoResize, @@ -956,11 +957,13 @@ ClearHtmlMarkersAsync, AddPopupAsync, Popup_RemoveAsync, - ClearPopups) + ClearPopupsAsync, + SetCameraOptionsAsync) { Controls = Controls, HtmlMarkers = HtmlMarkers, - DrawingToolbarOptions = DrawingToolbarOptions + DrawingToolbarOptions = DrawingToolbarOptions, + CameraOptions = cameraOptions }); await AddControlsAsync(Controls); @@ -970,6 +973,8 @@ await DispatchMapEventAsync(mapEvent); } + private async Task SetCameraOptionsAsync(CameraOptions options) => await JSRuntime.InvokeVoidAsync(Constants.JsConstants.MethodSetCameraOptions.ToAzureMapsControlNamespace(), options); + private async Task ClearMapAsync() => await JSRuntime.InvokeVoidAsync(Constants.JsConstants.MethodClearMap.ToAzureMapsControlNamespace()); private async Task DispatchMapEventAsync(MapJsEventArgs mapEvent) @@ -1140,7 +1145,7 @@ MapService.Map.RemovePopup(id); } private async Task Popup_UpdateAsync(string id, AzureMapsControl.Components.Popups.PopupOptions options) => await JSRuntime.InvokeVoidAsync(Constants.JsConstants.MethodPopupUpdate.ToAzureMapsControlNamespace(), id, options); - private async Task ClearPopups() => await JSRuntime.InvokeVoidAsync(Constants.JsConstants.MethodClearPopups.ToAzureMapsControlNamespace()); + private async Task ClearPopupsAsync() => await JSRuntime.InvokeVoidAsync(Constants.JsConstants.MethodClearPopups.ToAzureMapsControlNamespace()); #endregion } diff --git a/src/AzureMapsControl.Components/Map/CameraOptions.cs b/src/AzureMapsControl.Components/Map/CameraOptions.cs index d3b855a..469f9d7 100644 --- a/src/AzureMapsControl.Components/Map/CameraOptions.cs +++ b/src/AzureMapsControl.Components/Map/CameraOptions.cs @@ -5,7 +5,7 @@ using AzureMapsControl.Components.Atlas; [ExcludeFromCodeCoverage] - internal class CameraOptions + public sealed class CameraOptions { public int? Bearing { get; set; } public BoundingBox Bounds { get; set; } diff --git a/src/AzureMapsControl.Components/Map/Map.cs b/src/AzureMapsControl.Components/Map/Map.cs index e417a2c..1fa7911 100644 --- a/src/AzureMapsControl.Components/Map/Map.cs +++ b/src/AzureMapsControl.Components/Map/Map.cs @@ -37,6 +37,7 @@ public sealed class Map private readonly Func _addPopupCallback; private readonly Func _removePopupCallback; private readonly Func _clearPopupsCallback; + private readonly Func _setCameraCallback; private List _layers; private List _sources; @@ -59,6 +60,8 @@ public sealed class Map public IEnumerable Popups => _popups; + internal CameraOptions CameraOptions { get; set; } + internal Map(string id, Func, Task> addControlsCallback = null, Func, Task> addHtmlMarkersCallback = null, @@ -78,7 +81,8 @@ internal Map(string id, Func clearHtmlMarkersCallback = null, Func addPopupCallback = null, Func removePopupCallback = null, - Func clearPopupsCallback = null) + Func clearPopupsCallback = null, + Func setCameraCallback = null) { Id = id; _addControlsCallback = addControlsCallback; @@ -100,6 +104,7 @@ internal Map(string id, _addPopupCallback = addPopupCallback; _removePopupCallback = removePopupCallback; _clearPopupsCallback = clearPopupsCallback; + _setCameraCallback = setCameraCallback; } # region Controls @@ -391,6 +396,12 @@ public async Task ClearMapAsync() await _clearMapCallback.Invoke(); } + public async Task SetCameraOptionsAsync(Action optionsCallback) + { + optionsCallback.Invoke(CameraOptions); + await _setCameraCallback.Invoke(CameraOptions); + } + #endregion #region Popups diff --git a/src/AzureMapsControl.Components/wwwroot/azure-maps-control.js b/src/AzureMapsControl.Components/wwwroot/azure-maps-control.js index 4c595a0..50a819e 100644 --- a/src/AzureMapsControl.Components/wwwroot/azure-maps-control.js +++ b/src/AzureMapsControl.Components/wwwroot/azure-maps-control.js @@ -361,6 +361,15 @@ window.azureMapsControl = { userInteractionOptions, trafficOptions) { + this.setCameraOptions(cameraOptions); + this._map.setStyle(styleOptions); + this._map.setUserInteraction(userInteractionOptions); + + if (trafficOptions) { + this._map.setTraffic(trafficOptions); + } + }, + setCameraOptions: function (cameraOptions) { const options = { bearing: cameraOptions.bearing, centerOffset: cameraOptions.centerOffset, @@ -384,12 +393,6 @@ window.azureMapsControl = { } this._map.setCamera(options); - this._map.setStyle(styleOptions); - this._map.setUserInteraction(userInteractionOptions); - - if (trafficOptions) { - this._map.setTraffic(trafficOptions); - } }, removeHtmlMarkers: function (markerIds) { this._map.markers.remove(this._map.markers.getMarkers().find(marker => markerIds.indexOf(marker.amc.id) > -1)); diff --git a/tests/AzureMapsControl.Components.Tests/Map/Map.cs b/tests/AzureMapsControl.Components.Tests/Map/Map.cs index d1d7e92..d6c4d9b 100644 --- a/tests/AzureMapsControl.Components.Tests/Map/Map.cs +++ b/tests/AzureMapsControl.Components.Tests/Map/Map.cs @@ -9,6 +9,7 @@ using AzureMapsControl.Components.Drawing; using AzureMapsControl.Components.Exceptions; using AzureMapsControl.Components.Layers; + using AzureMapsControl.Components.Map; using AzureMapsControl.Components.Markers; using AzureMapsControl.Components.Popups; @@ -590,5 +591,21 @@ public async void Should_ClearPopups_Async() Assert.True(assertClearCallback); Assert.Null(map.Popups); } + + [Fact] + public async void Should_UpdateCameraOptions_Async() + { + var assertOptionsCallback = false; + var center = new Position(10, 10); + var initialCameraOptions = new CameraOptions { + Duration = 10 + }; + var map = new Map("id", setCameraCallback: async options => assertOptionsCallback = options.Center == center && options.Duration == initialCameraOptions.Duration) { + CameraOptions = initialCameraOptions + }; + + await map.SetCameraOptionsAsync(options => options.Center = center); + Assert.True(assertOptionsCallback); + } } }