diff --git a/blazorators.sln b/blazorators.sln index caf38bc..fddfafd 100644 --- a/blazorators.sln +++ b/blazorators.sln @@ -34,7 +34,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Blazor.Serialization", "src EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Blazor.Geolocation.WebAssembly", "src\Blazor.Geolocation.WebAssembly\Blazor.Geolocation.WebAssembly.csproj", "{0BAC9703-45EE-4E9E-A0F0-556F02FCB901}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Blazor.Geolocation.Server", "src\Blazor.Geolocation.Server\Blazor.Geolocation.Server.csproj", "{A9F3FE17-EF4C-44B8-B265-7D7DBAA42F84}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Blazor.Geolocation.Server", "src\Blazor.Geolocation.Server\Blazor.Geolocation.Server.csproj", "{A9F3FE17-EF4C-44B8-B265-7D7DBAA42F84}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/samples/Blazor.ExampleConsumer/Components/BingMap.razor b/samples/Blazor.ExampleConsumer/Components/BingMap.razor new file mode 100644 index 0000000..86c486a --- /dev/null +++ b/samples/Blazor.ExampleConsumer/Components/BingMap.razor @@ -0,0 +1,19 @@ +@inject IJSInProcessRuntime JavaScript + +
+ +@code { + [Parameter, EditorRequired] + public GeolocationPosition Position { get; set; } = null!; + + protected override void OnParametersSet() + { + if (Position is null or { Coords: null }) + { + return; + } + + JavaScript.InvokeVoid( + "app.loadMap", "map", Position.Coords.Latitude, Position.Coords.Longitude); + } +} diff --git a/samples/Blazor.ExampleConsumer/GlobalUsings.cs b/samples/Blazor.ExampleConsumer/GlobalUsings.cs new file mode 100644 index 0000000..83cb9f0 --- /dev/null +++ b/samples/Blazor.ExampleConsumer/GlobalUsings.cs @@ -0,0 +1,6 @@ +// Copyright (c) David Pine. All rights reserved. +// Licensed under the MIT License. + +global using System.Text.Json; +global using System.Text.Json.Serialization; +global using Microsoft.JSInterop; diff --git a/samples/Blazor.ExampleConsumer/Pages/ClientPosition.razor b/samples/Blazor.ExampleConsumer/Pages/ClientPosition.razor index 5a131c3..86de0b3 100644 --- a/samples/Blazor.ExampleConsumer/Pages/ClientPosition.razor +++ b/samples/Blazor.ExampleConsumer/Pages/ClientPosition.razor @@ -34,46 +34,6 @@ @json } -} -@code { - readonly JsonSerializerOptions _opts = new() - { - WriteIndented = true, - PropertyNamingPolicy = JsonNamingPolicy.CamelCase, - DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull - }; - readonly PositionOptions _options = new() - { - EnableHighAccuracy = true, - MaximumAge = 0, - Timeout = 15_000 - }; - - GeolocationPosition? _position; - GeolocationPositionError? _positionError; - bool _isLoading = true; - - protected override void OnInitialized() => - Geolocation.GetCurrentPosition( - component: this, - onSuccessCallbackMethodName: nameof(OnPositionRecieved), - onErrorCallbackMethodName: nameof(OnPositionError), - options: _options); - - [JSInvokable] - public void OnPositionRecieved(GeolocationPosition position) - { - _isLoading = false; - _position = position; - StateHasChanged(); - } - - [JSInvokable] - public void OnPositionError(GeolocationPositionError positionError) - { - _isLoading = false; - _positionError = positionError; - StateHasChanged(); - } -} + +} \ No newline at end of file diff --git a/samples/Blazor.ExampleConsumer/Pages/ClientPosition.razor.cs b/samples/Blazor.ExampleConsumer/Pages/ClientPosition.razor.cs new file mode 100644 index 0000000..9f0939d --- /dev/null +++ b/samples/Blazor.ExampleConsumer/Pages/ClientPosition.razor.cs @@ -0,0 +1,47 @@ +// Copyright (c) David Pine. All rights reserved. +// Licensed under the MIT License. + +namespace Blazor.ExampleConsumer.Pages; + +public partial class ClientPosition +{ + readonly JsonSerializerOptions _opts = new() + { + WriteIndented = true, + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull + }; + readonly PositionOptions _options = new() + { + EnableHighAccuracy = true, + MaximumAge = 0, + Timeout = 15_000 + }; + + GeolocationPosition? _position; + GeolocationPositionError? _positionError; + bool _isLoading = true; + + protected override void OnInitialized() => + Geolocation.GetCurrentPosition( + component: this, + onSuccessCallbackMethodName: nameof(OnPositionRecieved), + onErrorCallbackMethodName: nameof(OnPositionError), + options: _options); + + [JSInvokable] + public void OnPositionRecieved(GeolocationPosition position) + { + _isLoading = false; + _position = position; + StateHasChanged(); + } + + [JSInvokable] + public void OnPositionError(GeolocationPositionError positionError) + { + _isLoading = false; + _positionError = positionError; + StateHasChanged(); + } +} diff --git a/samples/Blazor.ExampleConsumer/_Imports.razor b/samples/Blazor.ExampleConsumer/_Imports.razor index 303cc05..48427fc 100644 --- a/samples/Blazor.ExampleConsumer/_Imports.razor +++ b/samples/Blazor.ExampleConsumer/_Imports.razor @@ -10,4 +10,5 @@ @using System.Text.Json.Serialization @using Blazor.ExampleConsumer @using Blazor.ExampleConsumer.Shared -@using Blazor.Serialization.Extensions; \ No newline at end of file +@using Blazor.ExampleConsumer.Components +@using Blazor.Serialization.Extensions \ No newline at end of file diff --git a/samples/Blazor.ExampleConsumer/wwwroot/app.js b/samples/Blazor.ExampleConsumer/wwwroot/app.js index 5f28270..3e21f26 100644 --- a/samples/Blazor.ExampleConsumer/wwwroot/app.js +++ b/samples/Blazor.ExampleConsumer/wwwroot/app.js @@ -1 +1,34 @@ - \ No newline at end of file +let _map = null; + +const loadMap = (mapId, latitude, longitude) => { + const element = document.getElementById(mapId); + if (!!element) { + const navigationBarMode = Microsoft.Maps.NavigationBarMode; + const location = new Microsoft.Maps.Location(latitude, longitude); + _map = new Microsoft.Maps.Map(element, { + center: location, + navigationBarMode: navigationBarMode.compact, + supportedMapTypes: [ + Microsoft.Maps.MapTypeId.road, + Microsoft.Maps.MapTypeId.aerial, + Microsoft.Maps.MapTypeId.canvasLight + ] + }); + _map.setView({ zoom: 18 }); + Microsoft.Maps.loadModule('Microsoft.Maps.Search', () => { + var searchManager = new Microsoft.Maps.Search.SearchManager(_map); + var reverseGeocodeRequestOptions = { + location: location, + callback: (answer, userData) => { + _map.setView({ bounds: answer.bestView }); + _map.entities.push(new Microsoft.Maps.Pushpin(reverseGeocodeRequestOptions.location)); + } + }; + searchManager.reverseGeocode(reverseGeocodeRequestOptions); + }); + } +}; + +window.app = { + loadMap +}; \ No newline at end of file diff --git a/samples/Blazor.ExampleConsumer/wwwroot/index.html b/samples/Blazor.ExampleConsumer/wwwroot/index.html index d67b43d..5841ab6 100644 --- a/samples/Blazor.ExampleConsumer/wwwroot/index.html +++ b/samples/Blazor.ExampleConsumer/wwwroot/index.html @@ -20,6 +20,8 @@ 🗙 + +