Skip to content

Commit

Permalink
Merge pull request #11 from arnaudleclerc/releases/0.5.0
Browse files Browse the repository at this point in the history
Adding SetCameraOptions on Map
  • Loading branch information
arnaudleclerc authored Nov 19, 2020
2 parents d46cf9e + 1441b90 commit 39cfdd3
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 27 deletions.
2 changes: 2 additions & 0 deletions samples/AzureMapsControl.Sample/Pages/HtmlMarkersRemove.razor
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@
(
_marker
);

await args.Map.SetCameraOptionsAsync(options => options.Center = args.Position);
}
}
1 change: 1 addition & 0 deletions src/AzureMapsControl.Components/Constants/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
43 changes: 24 additions & 19 deletions src/AzureMapsControl.Components/Map/AzureMap.razor
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -956,11 +957,13 @@
ClearHtmlMarkersAsync,
AddPopupAsync,
Popup_RemoveAsync,
ClearPopups)
ClearPopupsAsync,
SetCameraOptionsAsync)
{
Controls = Controls,
HtmlMarkers = HtmlMarkers,
DrawingToolbarOptions = DrawingToolbarOptions
DrawingToolbarOptions = DrawingToolbarOptions,
CameraOptions = cameraOptions
});

await AddControlsAsync(Controls);
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion src/AzureMapsControl.Components/Map/CameraOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
13 changes: 12 additions & 1 deletion src/AzureMapsControl.Components/Map/Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public sealed class Map
private readonly Func<Popup, Task> _addPopupCallback;
private readonly Func<string, Task> _removePopupCallback;
private readonly Func<Task> _clearPopupsCallback;
private readonly Func<CameraOptions, Task> _setCameraCallback;

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

public IEnumerable<Popup> Popups => _popups;

internal CameraOptions CameraOptions { get; set; }

internal Map(string id,
Func<IEnumerable<Control>, Task> addControlsCallback = null,
Func<IEnumerable<HtmlMarker>, Task> addHtmlMarkersCallback = null,
Expand All @@ -78,7 +81,8 @@ internal Map(string id,
Func<Task> clearHtmlMarkersCallback = null,
Func<Popup, Task> addPopupCallback = null,
Func<string, Task> removePopupCallback = null,
Func<Task> clearPopupsCallback = null)
Func<Task> clearPopupsCallback = null,
Func<CameraOptions, Task> setCameraCallback = null)
{
Id = id;
_addControlsCallback = addControlsCallback;
Expand All @@ -100,6 +104,7 @@ internal Map(string id,
_addPopupCallback = addPopupCallback;
_removePopupCallback = removePopupCallback;
_clearPopupsCallback = clearPopupsCallback;
_setCameraCallback = setCameraCallback;
}

# region Controls
Expand Down Expand Up @@ -391,6 +396,12 @@ public async Task ClearMapAsync()
await _clearMapCallback.Invoke();
}

public async Task SetCameraOptionsAsync(Action<CameraOptions> optionsCallback)
{
optionsCallback.Invoke(CameraOptions);
await _setCameraCallback.Invoke(CameraOptions);
}

#endregion

#region Popups
Expand Down
15 changes: 9 additions & 6 deletions src/AzureMapsControl.Components/wwwroot/azure-maps-control.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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));
Expand Down
17 changes: 17 additions & 0 deletions tests/AzureMapsControl.Components.Tests/Map/Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit 39cfdd3

Please sign in to comment.