Skip to content

Commit 22b31d4

Browse files
authored
Merge pull request #110 from majorimi/dev/google-map-restriction
Dev/google map restriction
2 parents aae48ba + a75b7b0 commit 22b31d4

File tree

10 files changed

+125
-25
lines changed

10 files changed

+125
-25
lines changed

.github/docs/Maps.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Maps using `IGeolocationService` (see "Dependences") to center current position.
3434
It can be omitted and injected separately to your components as well to get or track device location.
3535
To see how it works please check **Geo JS** [documentation](https://github.com/majorimi/blazor-components/blob/master/.github/docs/JsInterop.md#geolocation-js-see-demo-app) and [demo](https://blazorextensions.z6.web.core.windows.net/jsinterop#geo-js).
3636

37+
3738
## `GoogleStaticMap` component (See: [demo app](https://blazorextensions.z6.web.core.windows.net/maps#google-static-maps))
3839

3940
:warning: **To use Google Maps Platform, you must have a billing account. The billing account is used to track costs associated with your projects.**
@@ -98,7 +99,6 @@ Once operation has finished successfully `OnLocationDetected` event will be fire
9899
- **`DisposeAsync()`: `Task DisposeAsync()`** <br />
99100
Component implements `IAsyncDisposable` interface Blazor framework will call it when parent removed from render tree.
100101

101-
102102
## `GoogleMap` component (See: [demo app](https://blazorextensions.z6.web.core.windows.net/maps#google-js-maps))
103103

104104
:warning: **To use Google Maps Platform, you must have a billing account. The billing account is used to track costs associated with your projects.**

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

Lines changed: 9 additions & 1 deletion
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}'.");
@@ -136,7 +137,7 @@
136137
},
137138
mapZoomChangedCallback: async (zoom) =>
138139
{
139-
WriteDiag($"Map Zoom level changed to: '{zoom}'.");
140+
WriteDiag($"Map Zoom level callback changed to: '{zoom}'.");
140141
if (_zoomLevel == zoom)
141142
return;
142143

@@ -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>
@@ -330,6 +336,8 @@
330336
if (_mapInitialized && value != _zoomLevel)
331337
{
332338
_zoomLevel = value;
339+
340+
WriteDiag($"Map Zoom level property Set to: '{_zoomLevel}'.");
333341
InvokeAsync(async () => await _mapService.SetZoomAsync(_zoomLevel));
334342
}
335343
}

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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace Majorsoft.Blazor.Components.Maps.Google
2+
{
3+
/// <summary>
4+
/// Restrictions coordinates for <see cref="GoogleMap"/>
5+
/// NOTE: Google Maps restriction is basically a MAX Zoom level. So it does not allow users to zoom out (zoom level value forced).
6+
/// In order to notify Blazor about the maximum Zoom level two-way binding MUST be used: `@bind-Zoom="_jsMapZoomLevel" @bind-Zoom:event="OnMapZoomLevelChanged"`
7+
/// </summary>
8+
public class GoogleMapRestriction
9+
{
10+
/// <summary>
11+
/// Latitude and Longitude SW/NE corners of the bound.
12+
/// </summary>
13+
public GoogleMapLatLngBounds LatLngBounds { get; set; }
14+
15+
/// <summary>
16+
/// Is restriction strict.
17+
/// </summary>
18+
public bool StrictBounds { get; set; }
19+
}
20+
}

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: 37 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)