Skip to content

Commit 275eeb7

Browse files
committed
Implemented GoogleMap restriction boundaries in Init
1 parent 5d3eb52 commit 275eeb7

File tree

9 files changed

+112
-11
lines changed

9 files changed

+112
-11
lines changed

src/Majorsoft.Blazor.Components.Maps/Google/GoogleMap.razor

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
mapContainerId: _mapContainerId,
3838
backgroundColor: BackgroundColor,
3939
controlSize: ControlSize,
40+
restriction: Restriction,
4041
mapInitializedCallback: async (mapId) =>
4142
{
4243
WriteDiag($"Google JavaScript API Map initialzied with DIV Id: '{_mapContainerId}'.");
@@ -292,6 +293,11 @@
292293
/// This option can only be set when the map is initialized. Use <see cref="OnInitialized"/> method to set it up.
293294
/// </summary>
294295
[Parameter] public IEnumerable<GoogleMapCustomControl>? CustomControls { get; set; }
296+
/// <summary>
297+
/// Restrictions for Maps by coordinates SW/NE.
298+
/// This option can only be set when the map is initialized. Use <see cref="OnInitialized"/> method to set it up.
299+
/// </summary>
300+
[Parameter] public GoogleMapRestriction? Restriction { get; set; }
295301

296302
private ObservableRangeCollection<GoogleMapMarker>? _markers;
297303
/// <summary>

src/Majorsoft.Blazor.Components.Maps/Google/GoogleMapLatLngBounds.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,24 @@
55
/// </summary>
66
public class GoogleMapLatLngBounds
77
{
8+
/// <summary>
9+
/// Default constructor
10+
/// </summary>
11+
public GoogleMapLatLngBounds()
12+
{
13+
}
14+
15+
/// <summary>
16+
/// Initialize object for Map restrictions
17+
/// </summary>
18+
/// <param name="southWest">The south-west corner of this bounds.</param>
19+
/// <param name="northEast">The north-east corner of this bounds.</param>
20+
public GoogleMapLatLngBounds(GoogleMapLatLng southWest, GoogleMapLatLng northEast)
21+
{
22+
SouthWest = southWest;
23+
NorthEast = northEast;
24+
}
25+
826
/// <summary>
927
/// Computes the center of this LatLngBounds
1028
/// Equivalent with `getCenter()` method call but it would require JsInterop.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace Majorsoft.Blazor.Components.Maps.Google
2+
{
3+
/// <summary>
4+
/// Restrictions coordinates for <see cref="GoogleMap"/>
5+
/// </summary>
6+
public class GoogleMapRestriction
7+
{
8+
/// <summary>
9+
/// Latitude and Longitude SW/NE corners of the bound.
10+
/// </summary>
11+
public GoogleMapLatLngBounds LatLngBounds { get; set; }
12+
13+
/// <summary>
14+
/// Is restriction strict.
15+
/// </summary>
16+
public bool StrictBounds { get; set; }
17+
}
18+
}

src/Majorsoft.Blazor.Components.Maps/Google/GoogleMapService.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ public async Task InitMapAsync(string apiKey,
5151
Func<GeolocationCoordinate, Task>? mapDragStartCallback = null,
5252
Func<Rect, Task>? mapResizedCallback = null,
5353
Func<Task>? mapTilesLoadedCallback = null,
54-
Func<Task>? mapIdleCallback = null)
54+
Func<Task>? mapIdleCallback = null,
55+
GoogleMapRestriction restriction = null)
5556
{
5657
if(MapContainerId == mapContainerId)
5758
{
@@ -89,7 +90,7 @@ public async Task InitMapAsync(string apiKey,
8990

9091
_dotNetObjectReference = DotNetObjectReference.Create<GoogleMapEventInfo>(info);
9192

92-
await _mapsJs.InvokeVoidAsync("init", apiKey, mapContainerId, _dotNetObjectReference, backgroundColor, controlSize);
93+
await _mapsJs.InvokeVoidAsync("init", apiKey, mapContainerId, _dotNetObjectReference, backgroundColor, controlSize, restriction);
9394
}
9495

9596
public async Task SetCenterAsync(double latitude, double longitude)

src/Majorsoft.Blazor.Components.Maps/Google/IGoogleMapService.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ Task InitMapAsync(string apiKey,
7676
Func<GeolocationCoordinate, Task> mapDragStartCallback = null,
7777
Func<Rect, Task> mapResizedCallback = null,
7878
Func<Task> mapTilesLoadedCallback = null,
79-
Func<Task> mapIdleCallback = null);
79+
Func<Task> mapIdleCallback = null,
80+
GoogleMapRestriction restriction = null);
8081

8182
/// <summary>
8283
/// Sets the center point as coordinates of the Map.

src/Majorsoft.Blazor.Components.Maps/Majorsoft.Blazor.Components.Maps.xml

Lines changed: 35 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Majorsoft.Blazor.Components.Maps/wwwroot/googleMaps.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
export function init(key, elementId, dotnetRef, backgroundColor, controlSize) {
1+
export function init(key, elementId, dotnetRef, backgroundColor, controlSize, restriction) {
22
if (!key || !elementId || !dotnetRef) {
33
return;
44
}
55

6-
storeElementIdWithDotnetRef(_mapsElementDict, elementId, dotnetRef, backgroundColor, controlSize); //Store map info
6+
storeElementIdWithDotnetRef(_mapsElementDict, elementId, dotnetRef, backgroundColor, controlSize, restriction); //Store map info
77

88
let src = "https://maps.googleapis.com/maps/api/js?key=";
99
let scriptsIncluded = false;
@@ -50,9 +50,25 @@ window.initGoogleMaps = () => {
5050
}
5151

5252
//Create Map
53+
let restrict = null;
54+
if (mapInfo.restriction && mapInfo.restriction.latLngBounds
55+
&& mapInfo.restriction.latLngBounds.northEast && mapInfo.restriction.latLngBounds.southWest) {
56+
restrict =
57+
{
58+
latLngBounds: {
59+
south: mapInfo.restriction.latLngBounds.southWest.latitude,
60+
west: mapInfo.restriction.latLngBounds.southWest.longitude,
61+
north: mapInfo.restriction.latLngBounds.northEast.latitude,
62+
east: mapInfo.restriction.latLngBounds.northEast.longitude
63+
},
64+
strictBounds: mapInfo.restriction.strictBounds,
65+
};
66+
}
67+
5368
let map = new google.maps.Map(document.getElementById(elementId), {
5469
backgroundColor: mapInfo.bgColor,
5570
controlSize: mapInfo.ctrSize,
71+
restriction: restrict,
5672
});
5773
map.elementId = elementId;
5874
_mapsElementDict[i].value.map = map;
@@ -265,7 +281,7 @@ window.initGoogleMaps = () => {
265281
};
266282

267283
//Store elementId with .NET Ref
268-
function storeElementIdWithDotnetRef(dict, elementId, dotnetRef, backgroundColor, controlSize) {
284+
function storeElementIdWithDotnetRef(dict, elementId, dotnetRef, backgroundColor, controlSize, restriction) {
269285
let elementFound = false;
270286
for (let i = 0; i < dict.length; i++) {
271287
if (dict[i].key === elementId) {
@@ -276,7 +292,7 @@ function storeElementIdWithDotnetRef(dict, elementId, dotnetRef, backgroundColor
276292
if (!elementFound) {
277293
dict.push({
278294
key: elementId,
279-
value: { ref: dotnetRef, map: null, bgColor: backgroundColor, ctrSize: controlSize }
295+
value: { ref: dotnetRef, map: null, bgColor: backgroundColor, ctrSize: controlSize, restriction: restriction }
280296
});
281297
}
282298
}

0 commit comments

Comments
 (0)