Skip to content

Commit aae48ba

Browse files
authored
Merge pull request #108 from majorimi/dev/google-maps-methods
GoogleMaps get methods
2 parents b9ac1be + 5d3eb52 commit aae48ba

File tree

10 files changed

+256
-14
lines changed

10 files changed

+256
-14
lines changed

.github/docs/Maps.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,16 @@ It is available in the service provider (Google, Microsoft, etc.) developer site
1919
**NOTE: None of the Majorsoft Maps component tracking or exposing you _Token_ or _API Key_!
2020
Injecting and protecting this _Token_ or _API Key_ in your Blazor application is YOUR responsibility!**
2121

22-
# Components
22+
# Components and Services
2323

24+
25+
#### Google:
2426
- **`GoogleStaticMap`**: component is wrapping **Google Static Maps services** into Blazor components.
2527
- **`GoogleMap`**: component is wrapping **Google JavaScript Maps services** into Blazor components.
26-
- **`BindMap`**: _Planned in release v1.4.0_
28+
- **`IGoogleMapService`**: Injectable service to handle Google JavaScript Maps functionalities. Available on the instance of `GoogleMap` object ref as well.
29+
30+
#### Bing:
31+
- **`BindMap`**: _Planned in release v1.6.0_
2732

2833
Maps using `IGeolocationService` (see "Dependences") to center current position.
2934
It can be omitted and injected separately to your components as well to get or track device location.
@@ -111,6 +116,8 @@ You can learn about Google JavaScript Maps features and usage [here](https://dev
111116
Exposes a Blazor `ElementReference` of the wrapped around HTML element. It can be used e.g. for JS interop, etc.
112117
- **`MapId`: `string { get; }`** <br />
113118
Map HTML container Id. It can be used when multiple Maps added to one page.
119+
- **`GoogleMapService`: `string { get; }`** <br />
120+
Exposes `IGeolocationService` which is handling JsInterop. This instance can be used for access more GoogleMap features.
114121
- **`Width`: `int { get; set; }` (default: 400)** <br />
115122
Maps image Width in px.
116123
- **`Height`: `int { get; set; }` (default: 300)** <br />

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
/// </summary>
2424
public ElementReference InnerElementReference => _jsMap;
2525

26+
/// <summary>
27+
/// Exposes <see cref="IGeolocationService"/> which is handling JsInterop. This instance can be used for access more GoogleMap features.
28+
/// </summary>
29+
public IGoogleMapService GoogleMapService => _mapService;
30+
2631
private bool _mapInitialized = false;
2732
private bool _isDragging = false;
2833
protected override async Task OnInitializedAsync()
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
namespace Majorsoft.Blazor.Components.Maps.Google
2+
{
3+
/// <summary>
4+
/// A LatLngBounds instance represents a rectangle in geographical coordinates, including one that crosses the 180 degrees longitudinal meridian.
5+
/// </summary>
6+
public class GoogleMapLatLngBounds
7+
{
8+
/// <summary>
9+
/// Computes the center of this LatLngBounds
10+
/// Equivalent with `getCenter()` method call but it would require JsInterop.
11+
/// </summary>
12+
public GoogleMapLatLng Center { get; set; }
13+
14+
/// <summary>
15+
/// Returns the north-east corner of this bounds.
16+
/// Equivalent with `getNorthEast()` method call but it would require JsInterop.
17+
/// </summary>
18+
public GoogleMapLatLng NorthEast { get; set; }
19+
20+
/// <summary>
21+
/// Returns the south-west corner of this bounds.
22+
/// Equivalent with `getSouthWest()` method call but it would require JsInterop.
23+
/// </summary>
24+
public GoogleMapLatLng SouthWest { get; set; }
25+
26+
/// <summary>
27+
/// Converts the given map bounds to a lat/lng span.
28+
/// Equivalent with `toSpan()` method call but it would require JsInterop.
29+
/// </summary>
30+
public GoogleMapLatLng Span { get; set; }
31+
32+
/// <summary>
33+
/// Returns if the bounds are empty.
34+
/// Equivalent with `isEmpty()` method call but it would require JsInterop.
35+
/// </summary>
36+
public bool IsEmpty { get; set; }
37+
}
38+
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,25 @@ await _mapsJs.InvokeVoidAsync("removeMarkers", MapContainerId,
236236
}
237237
}
238238

239+
public async ValueTask<GoogleMapLatLngBounds> GetBoundsAsync()
240+
{
241+
await CheckJsObjectAsync();
242+
return await _mapsJs.InvokeAsync<GoogleMapLatLngBounds>("getBounds", MapContainerId);
243+
}
244+
245+
public async ValueTask<GoogleMapLatLng> GetCenterAsync()
246+
{
247+
await CheckJsObjectAsync();
248+
return await _mapsJs.InvokeAsync<GoogleMapLatLng>("getCenter", MapContainerId);
249+
}
250+
251+
public async ValueTask<IJSObjectReference> GetDivAsync()
252+
{
253+
await CheckJsObjectAsync();
254+
return await _mapsJs.InvokeAsync<IJSObjectReference>("getDiv", MapContainerId);
255+
}
256+
257+
239258
private async Task CheckJsObjectAsync()
240259
{
241260
if (_mapsJs is null)

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

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-

2-
using System;
1+
using System;
32
using System.Collections.Generic;
43
using System.Dynamic;
54
using System.Threading.Tasks;
65

6+
using Microsoft.JSInterop;
7+
78
namespace Majorsoft.Blazor.Components.Maps.Google
89
{
910
/// <summary>
10-
/// Injectable service to handle Google JavaScript Maps functionalities.
11+
/// Injectable service to handle Google JavaScript Maps functionalities. Available on the instance of <see cref="GoogleMap"/> object ref as well.
1112
/// </summary>
1213
public interface IGoogleMapService : IAsyncDisposable
1314
{
@@ -169,7 +170,29 @@ Task InitMapAsync(string apiKey,
169170
/// </summary>
170171
/// <param name="newMarkers">Enumerable new markers to add</param>
171172
/// <param name="markers">Enumerable markers removed or replaced</param>
172-
/// <returns></returns>
173+
/// <returns>Async task</returns>
173174
Task CreateMarkersAsync(IEnumerable<GoogleMapMarker>? newMarkers, IEnumerable<GoogleMapMarker>? markers);
175+
176+
/// <summary>
177+
/// Returns the lat/lng bounds of the current viewport. If more than one copy of the world is visible,
178+
/// the bounds range in longitude from -180 to 180 degrees inclusive. If the map is not yet initialized or
179+
/// center and zoom have not been set then the result is undefined. For vector maps with non-zero tilt or heading,
180+
/// the returned lat/lng bounds represents the smallest bounding box that includes the visible region of the map's viewport.
181+
/// </summary>
182+
/// <returns>Async task</returns>
183+
ValueTask<GoogleMapLatLngBounds> GetBoundsAsync();
184+
185+
/// <summary>
186+
/// Returns the position displayed at the center of the map. Note that this LatLng object is not wrapped. See LatLng for more information.
187+
/// If the center or bounds have not been set then the result is undefined.
188+
/// </summary>
189+
/// <returns>Async task</returns>
190+
ValueTask<GoogleMapLatLng> GetCenterAsync();
191+
192+
/// <summary>
193+
/// Returns HTMLElement The mapDiv of the map.
194+
/// </summary>
195+
/// <returns>Async task</returns>
196+
ValueTask<IJSObjectReference> GetDivAsync();
174197
}
175198
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace Majorsoft.Blazor.Components.Maps
2+
{
3+
/// <summary>
4+
/// A LatLng is a point in geographical coordinates: latitude and longitude.
5+
/// </summary>
6+
public class GoogleMapLatLng
7+
{
8+
/// <summary>
9+
/// Latitude ranges between -90 and 90 degrees, inclusive. Values above or below this range will be clamped to the range [-90, 90].
10+
/// This means that if the value specified is less than -90, it will be set to -90. And if the value is greater than 90, it will be set to 90.
11+
/// </summary>
12+
public double Latitude { get; set; }
13+
14+
/// <summary>
15+
/// Longitude ranges between -180 and 180 degrees, inclusive. Values above or below this range will be wrapped so that they fall within the range.
16+
/// For example, a value of -190 will be converted to 170. A value of 190 will be converted to -170. This reflects the fact that longitudes wrap around the globe.
17+
/// </summary>
18+
public double Longitude { get; set; }
19+
}
20+
}

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

Lines changed: 81 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: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,49 @@ export function panToAddress(elementId, address) {
342342
}
343343
}
344344
}
345+
//get methods
346+
export function getBounds(elementId) {
347+
if (elementId) {
348+
let mapWithDotnetRef = getElementIdWithDotnetRef(_mapsElementDict, elementId);
349+
if (mapWithDotnetRef && mapWithDotnetRef.map) {
350+
let bounds = mapWithDotnetRef.map.getBounds();
351+
352+
let ret = {
353+
Center: convertToLatLng(bounds.getCenter()),
354+
NorthEast: convertToLatLng(bounds.getNorthEast()),
355+
SouthWest: convertToLatLng(bounds.getSouthWest()),
356+
Span: convertToLatLng(bounds.toSpan()),
357+
IsEmpty: bounds.isEmpty(),
358+
};
359+
return ret;
360+
}
361+
}
362+
}
363+
export function getCenter(elementId) {
364+
if (elementId) {
365+
let mapWithDotnetRef = getElementIdWithDotnetRef(_mapsElementDict, elementId);
366+
if (mapWithDotnetRef && mapWithDotnetRef.map) {
367+
let center = mapWithDotnetRef.map.getCenter();
368+
369+
let ret = convertToLatLng(center);
370+
return ret;
371+
}
372+
}
373+
}
374+
export function getDiv(elementId) {
375+
if (elementId) {
376+
let mapWithDotnetRef = getElementIdWithDotnetRef(_mapsElementDict, elementId);
377+
if (mapWithDotnetRef && mapWithDotnetRef.map) {
378+
var ret = mapWithDotnetRef.map.getDiv();
379+
return ret;
380+
}
381+
}
382+
}
383+
function convertToLatLng(latLngObject) {
384+
let ret = { Latitude: latLngObject.lat(), Longitude: latLngObject.lng() };
385+
return ret;
386+
}
387+
345388
//set methods
346389
export function setZoom(elementId, zoom) {
347390
if (elementId) {

0 commit comments

Comments
 (0)