Skip to content

Commit 39cfdd3

Browse files
Merge pull request #11 from arnaudleclerc/releases/0.5.0
Adding SetCameraOptions on Map
2 parents d46cf9e + 1441b90 commit 39cfdd3

7 files changed

Lines changed: 66 additions & 27 deletions

File tree

samples/AzureMapsControl.Sample/Pages/HtmlMarkersRemove.razor

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,7 @@
2727
(
2828
_marker
2929
);
30+
31+
await args.Map.SetCameraOptionsAsync(options => options.Center = args.Position);
3032
}
3133
}

src/AzureMapsControl.Components/Constants/Constants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ internal static class JsConstants
1313
internal const string MethodAddMap = "addMap";
1414
internal const string MethodClearMap = "clearMap";
1515
internal const string MethodSetOptions = "setOptions";
16+
internal const string MethodSetCameraOptions = "setCameraOptions";
1617

1718
internal const string MethodAddControl = "addControls";
1819

src/AzureMapsControl.Components/Map/AzureMap.razor

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -888,23 +888,24 @@
888888
{
889889
if (mapEvent.Type == "ready")
890890
{
891+
var cameraOptions = new CameraOptions
892+
{
893+
Bearing = (int?)Bearing,
894+
CenterOffset = CenterOffset,
895+
Duration = Duration,
896+
MaxZoom = MaxZoom,
897+
MinZoom = MinZoom,
898+
Pitch = Pitch,
899+
Type = CameryType?.ToString(),
900+
Bounds = Bounds,
901+
MaxBounds = Bounds != null ? MaxBounds : null,
902+
Offset = Bounds != null ? Offset : null,
903+
Padding = Bounds != null ? Padding : null,
904+
Center = Bounds == null ? Center : null,
905+
Zoom = Bounds == null ? Zoom : null
906+
};
891907
await JSRuntime.InvokeVoidAsync(Constants.JsConstants.MethodSetOptions.ToAzureMapsControlNamespace(),
892-
new CameraOptions
893-
{
894-
Bearing = (int?)Bearing,
895-
CenterOffset = CenterOffset,
896-
Duration = Duration,
897-
MaxZoom = MaxZoom,
898-
MinZoom = MinZoom,
899-
Pitch = Pitch,
900-
Type = CameryType?.ToString(),
901-
Bounds = Bounds,
902-
MaxBounds = Bounds != null ? MaxBounds : null,
903-
Offset = Bounds != null ? Offset : null,
904-
Padding = Bounds != null ? Padding : null,
905-
Center = Bounds == null ? Center : null,
906-
Zoom = Bounds == null ? Zoom : null
907-
},
908+
cameraOptions,
908909
new StyleOptions
909910
{
910911
AutoResize = AutoResize,
@@ -956,11 +957,13 @@
956957
ClearHtmlMarkersAsync,
957958
AddPopupAsync,
958959
Popup_RemoveAsync,
959-
ClearPopups)
960+
ClearPopupsAsync,
961+
SetCameraOptionsAsync)
960962
{
961963
Controls = Controls,
962964
HtmlMarkers = HtmlMarkers,
963-
DrawingToolbarOptions = DrawingToolbarOptions
965+
DrawingToolbarOptions = DrawingToolbarOptions,
966+
CameraOptions = cameraOptions
964967
});
965968

966969
await AddControlsAsync(Controls);
@@ -970,6 +973,8 @@
970973
await DispatchMapEventAsync(mapEvent);
971974
}
972975

976+
private async Task SetCameraOptionsAsync(CameraOptions options) => await JSRuntime.InvokeVoidAsync(Constants.JsConstants.MethodSetCameraOptions.ToAzureMapsControlNamespace(), options);
977+
973978
private async Task ClearMapAsync() => await JSRuntime.InvokeVoidAsync(Constants.JsConstants.MethodClearMap.ToAzureMapsControlNamespace());
974979

975980
private async Task DispatchMapEventAsync(MapJsEventArgs mapEvent)
@@ -1140,7 +1145,7 @@
11401145
MapService.Map.RemovePopup(id);
11411146
}
11421147
private async Task Popup_UpdateAsync(string id, AzureMapsControl.Components.Popups.PopupOptions options) => await JSRuntime.InvokeVoidAsync(Constants.JsConstants.MethodPopupUpdate.ToAzureMapsControlNamespace(), id, options);
1143-
private async Task ClearPopups() => await JSRuntime.InvokeVoidAsync(Constants.JsConstants.MethodClearPopups.ToAzureMapsControlNamespace());
1148+
private async Task ClearPopupsAsync() => await JSRuntime.InvokeVoidAsync(Constants.JsConstants.MethodClearPopups.ToAzureMapsControlNamespace());
11441149

11451150
#endregion
11461151
}

src/AzureMapsControl.Components/Map/CameraOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using AzureMapsControl.Components.Atlas;
66

77
[ExcludeFromCodeCoverage]
8-
internal class CameraOptions
8+
public sealed class CameraOptions
99
{
1010
public int? Bearing { get; set; }
1111
public BoundingBox Bounds { get; set; }

src/AzureMapsControl.Components/Map/Map.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public sealed class Map
3737
private readonly Func<Popup, Task> _addPopupCallback;
3838
private readonly Func<string, Task> _removePopupCallback;
3939
private readonly Func<Task> _clearPopupsCallback;
40+
private readonly Func<CameraOptions, Task> _setCameraCallback;
4041

4142
private List<Layer> _layers;
4243
private List<Data.Source> _sources;
@@ -59,6 +60,8 @@ public sealed class Map
5960

6061
public IEnumerable<Popup> Popups => _popups;
6162

63+
internal CameraOptions CameraOptions { get; set; }
64+
6265
internal Map(string id,
6366
Func<IEnumerable<Control>, Task> addControlsCallback = null,
6467
Func<IEnumerable<HtmlMarker>, Task> addHtmlMarkersCallback = null,
@@ -78,7 +81,8 @@ internal Map(string id,
7881
Func<Task> clearHtmlMarkersCallback = null,
7982
Func<Popup, Task> addPopupCallback = null,
8083
Func<string, Task> removePopupCallback = null,
81-
Func<Task> clearPopupsCallback = null)
84+
Func<Task> clearPopupsCallback = null,
85+
Func<CameraOptions, Task> setCameraCallback = null)
8286
{
8387
Id = id;
8488
_addControlsCallback = addControlsCallback;
@@ -100,6 +104,7 @@ internal Map(string id,
100104
_addPopupCallback = addPopupCallback;
101105
_removePopupCallback = removePopupCallback;
102106
_clearPopupsCallback = clearPopupsCallback;
107+
_setCameraCallback = setCameraCallback;
103108
}
104109

105110
# region Controls
@@ -391,6 +396,12 @@ public async Task ClearMapAsync()
391396
await _clearMapCallback.Invoke();
392397
}
393398

399+
public async Task SetCameraOptionsAsync(Action<CameraOptions> optionsCallback)
400+
{
401+
optionsCallback.Invoke(CameraOptions);
402+
await _setCameraCallback.Invoke(CameraOptions);
403+
}
404+
394405
#endregion
395406

396407
#region Popups

src/AzureMapsControl.Components/wwwroot/azure-maps-control.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,15 @@ window.azureMapsControl = {
361361
userInteractionOptions,
362362
trafficOptions) {
363363

364+
this.setCameraOptions(cameraOptions);
365+
this._map.setStyle(styleOptions);
366+
this._map.setUserInteraction(userInteractionOptions);
367+
368+
if (trafficOptions) {
369+
this._map.setTraffic(trafficOptions);
370+
}
371+
},
372+
setCameraOptions: function (cameraOptions) {
364373
const options = {
365374
bearing: cameraOptions.bearing,
366375
centerOffset: cameraOptions.centerOffset,
@@ -384,12 +393,6 @@ window.azureMapsControl = {
384393
}
385394

386395
this._map.setCamera(options);
387-
this._map.setStyle(styleOptions);
388-
this._map.setUserInteraction(userInteractionOptions);
389-
390-
if (trafficOptions) {
391-
this._map.setTraffic(trafficOptions);
392-
}
393396
},
394397
removeHtmlMarkers: function (markerIds) {
395398
this._map.markers.remove(this._map.markers.getMarkers().find(marker => markerIds.indexOf(marker.amc.id) > -1));

tests/AzureMapsControl.Components.Tests/Map/Map.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using AzureMapsControl.Components.Drawing;
1010
using AzureMapsControl.Components.Exceptions;
1111
using AzureMapsControl.Components.Layers;
12+
using AzureMapsControl.Components.Map;
1213
using AzureMapsControl.Components.Markers;
1314
using AzureMapsControl.Components.Popups;
1415

@@ -590,5 +591,21 @@ public async void Should_ClearPopups_Async()
590591
Assert.True(assertClearCallback);
591592
Assert.Null(map.Popups);
592593
}
594+
595+
[Fact]
596+
public async void Should_UpdateCameraOptions_Async()
597+
{
598+
var assertOptionsCallback = false;
599+
var center = new Position(10, 10);
600+
var initialCameraOptions = new CameraOptions {
601+
Duration = 10
602+
};
603+
var map = new Map("id", setCameraCallback: async options => assertOptionsCallback = options.Center == center && options.Duration == initialCameraOptions.Duration) {
604+
CameraOptions = initialCameraOptions
605+
};
606+
607+
await map.SetCameraOptionsAsync(options => options.Center = center);
608+
Assert.True(assertOptionsCallback);
609+
}
593610
}
594611
}

0 commit comments

Comments
 (0)