diff --git a/.gitignore b/.gitignore index 3e759b7..0b1e718 100644 --- a/.gitignore +++ b/.gitignore @@ -328,3 +328,4 @@ ASALocalRun/ # MFractors (Xamarin productivity tool) working folder .mfractor/ +demo/Blazor.GoogleMap.Server/appsettings.Development.json diff --git a/Blazor.GoogleMap.sln b/Blazor.GoogleMap.sln index 4340136..4045624 100644 --- a/Blazor.GoogleMap.sln +++ b/Blazor.GoogleMap.sln @@ -3,12 +3,6 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 16 VisualStudioVersion = 16.0.28902.138 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Blazor.GoogleMap.Server", "demo\Blazor.GoogleMap.Server\Blazor.GoogleMap.Server.csproj", "{9D7D3AA1-A81E-454B-9B17-0B35CDBDCC3C}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Blazor.GoogleMap.Client", "demo\Blazor.GoogleMap.Client\Blazor.GoogleMap.Client.csproj", "{8015065B-010D-420B-9539-518D042BA0B4}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Blazor.GoogleMap.Shared", "demo\Blazor.GoogleMap.Shared\Blazor.GoogleMap.Shared.csproj", "{5265BA8E-F1A9-436A-9A19-2AC97373E3D2}" -EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "demo", "demo", "{A76C4CD5-7315-48CE-8A01-122B0662BAA5}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{EB447513-2400-44A6-B2B1-A3E53EBFC999}" @@ -20,37 +14,29 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "_solutionItems", "_solution README.md = README.md EndProjectSection EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Blazor.GoogleMap.Server", "demo\Blazor.GoogleMap.Server\Blazor.GoogleMap.Server.csproj", "{72279C45-BCD5-4F61-8E2B-179DED8BE123}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {9D7D3AA1-A81E-454B-9B17-0B35CDBDCC3C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {9D7D3AA1-A81E-454B-9B17-0B35CDBDCC3C}.Debug|Any CPU.Build.0 = Debug|Any CPU - {9D7D3AA1-A81E-454B-9B17-0B35CDBDCC3C}.Release|Any CPU.ActiveCfg = Release|Any CPU - {9D7D3AA1-A81E-454B-9B17-0B35CDBDCC3C}.Release|Any CPU.Build.0 = Release|Any CPU - {8015065B-010D-420B-9539-518D042BA0B4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {8015065B-010D-420B-9539-518D042BA0B4}.Debug|Any CPU.Build.0 = Debug|Any CPU - {8015065B-010D-420B-9539-518D042BA0B4}.Release|Any CPU.ActiveCfg = Release|Any CPU - {8015065B-010D-420B-9539-518D042BA0B4}.Release|Any CPU.Build.0 = Release|Any CPU - {5265BA8E-F1A9-436A-9A19-2AC97373E3D2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {5265BA8E-F1A9-436A-9A19-2AC97373E3D2}.Debug|Any CPU.Build.0 = Debug|Any CPU - {5265BA8E-F1A9-436A-9A19-2AC97373E3D2}.Release|Any CPU.ActiveCfg = Release|Any CPU - {5265BA8E-F1A9-436A-9A19-2AC97373E3D2}.Release|Any CPU.Build.0 = Release|Any CPU {8833072A-796E-43D9-840C-346F4C80409E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {8833072A-796E-43D9-840C-346F4C80409E}.Debug|Any CPU.Build.0 = Debug|Any CPU {8833072A-796E-43D9-840C-346F4C80409E}.Release|Any CPU.ActiveCfg = Release|Any CPU {8833072A-796E-43D9-840C-346F4C80409E}.Release|Any CPU.Build.0 = Release|Any CPU + {72279C45-BCD5-4F61-8E2B-179DED8BE123}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {72279C45-BCD5-4F61-8E2B-179DED8BE123}.Debug|Any CPU.Build.0 = Debug|Any CPU + {72279C45-BCD5-4F61-8E2B-179DED8BE123}.Release|Any CPU.ActiveCfg = Release|Any CPU + {72279C45-BCD5-4F61-8E2B-179DED8BE123}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(NestedProjects) = preSolution - {9D7D3AA1-A81E-454B-9B17-0B35CDBDCC3C} = {A76C4CD5-7315-48CE-8A01-122B0662BAA5} - {8015065B-010D-420B-9539-518D042BA0B4} = {A76C4CD5-7315-48CE-8A01-122B0662BAA5} - {5265BA8E-F1A9-436A-9A19-2AC97373E3D2} = {A76C4CD5-7315-48CE-8A01-122B0662BAA5} {8833072A-796E-43D9-840C-346F4C80409E} = {EB447513-2400-44A6-B2B1-A3E53EBFC999} + {72279C45-BCD5-4F61-8E2B-179DED8BE123} = {A76C4CD5-7315-48CE-8A01-122B0662BAA5} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {9FE02341-2660-48BD-A738-C721556C415E} diff --git a/README.md b/README.md index 81c0651..ade2216 100644 --- a/README.md +++ b/README.md @@ -1,23 +1,37 @@ # Blazor.GoogleMap -Blazor component for GoogleMap which allows to manipulate with GoogleMap just with C# language and no JS is required. +Blazor(Server) component for Google Map which allows to mapping features just with C# language and ASP.NET Core 3.0 + +Blazor Web Assembly is still in preview for ASP.NET Core 3.0 and its' API(s) are still changing a lot. So Blazor.GoogleMap.Client project is removed for now. ## IMPORTANT! -This is very very first preview version and API it will increase and also documentation. +This is just a preview version of the component but ready to use in PROD at your own risj. +In future new features will be added. Feel free to send PR(s). # Instalation -[![NuGet Pre Release](https://img.shields.io/badge/nuget-0.0.2--preview-orange.svg)](https://www.nuget.org/packages/BlazorMap) +Soon # Setup +Just add ``AddGoogleMaps`` service extension to default service collection of the application. Also don't forget to check other options. + ```cs public void ConfigureServices(IServiceCollection services) { services.AddGoogleMaps(options => { - options.ApiKey = "Your Google Maps Api Key"; + options.ApiKey = "Your Google Maps Api Key"; }); } ``` +Also don't forget to add following .js files into _Host.cshtml file as below + +```html + + + + +``` + # Features * Add marker to the map * Add OnClick, OnDoubleClick event listeners on the map and marker @@ -37,10 +51,11 @@ If you add marker into map whit filled property **AssociatedInfoWindowId**, afte @page "/map" @inject MarkerCollectionFactory MarkerCollectionFactory; @inject InfoWindow InfoWindow; +@inject IJSRuntime JsRuntime;

Google Map

- +

Infowindow 1

@@ -50,7 +65,7 @@ If you add marker into map whit filled property **AssociatedInfoWindowId**, afte } - +
@@ -63,7 +78,7 @@ If you add marker into map whit filled property **AssociatedInfoWindowId**, afte } - + @@ -72,9 +87,9 @@ If you add marker into map whit filled property **AssociatedInfoWindowId**, afte IMarkerCollection markers; Marker selectedMarker; - protected override void OnInit() + protected async override Task OnInitializedAsync() { - base.OnInit(); + await base.OnInitializedAsync(); markers = MarkerCollectionFactory.Create(); } diff --git a/demo/Blazor.GoogleMap.Client/App.razor b/demo/Blazor.GoogleMap.Client/App.razor deleted file mode 100644 index 80fb778..0000000 --- a/demo/Blazor.GoogleMap.Client/App.razor +++ /dev/null @@ -1 +0,0 @@ - diff --git a/demo/Blazor.GoogleMap.Client/Blazor.GoogleMap.Client.csproj b/demo/Blazor.GoogleMap.Client/Blazor.GoogleMap.Client.csproj deleted file mode 100644 index 05d1f26..0000000 --- a/demo/Blazor.GoogleMap.Client/Blazor.GoogleMap.Client.csproj +++ /dev/null @@ -1,24 +0,0 @@ - - - - netstandard2.0 - Exe - - https://dotnet.myget.org/F/aspnetcore-dev/api/v3/index.json; - https://dotnet.myget.org/F/blazor-dev/api/v3/index.json; - - 7.3 - 3.0 - - - - - - - - - - - - - diff --git a/demo/Blazor.GoogleMap.Client/Pages/FetchData.razor b/demo/Blazor.GoogleMap.Client/Pages/FetchData.razor deleted file mode 100644 index b82f1a2..0000000 --- a/demo/Blazor.GoogleMap.Client/Pages/FetchData.razor +++ /dev/null @@ -1,45 +0,0 @@ -@page "/fetchdata" -@using Blazor.GoogleMap.Shared -@inject HttpClient Http - -

Weather forecast

- -

This component demonstrates fetching data from the server.

- -@if (forecasts == null) -{ -

Loading...

-} -else -{ - - - - - - - - - - - @foreach (var forecast in forecasts) - { - - - - - - - } - -
DateTemp. (C)Temp. (F)Summary
@forecast.Date.ToShortDateString()@forecast.TemperatureC@forecast.TemperatureF@forecast.Summary
-} - -@functions { - WeatherForecast[] forecasts; - - protected override async Task OnInitAsync() - { - forecasts = await Http.GetJsonAsync("api/SampleData/WeatherForecasts"); - } -} diff --git a/demo/Blazor.GoogleMap.Client/Pages/Index.razor b/demo/Blazor.GoogleMap.Client/Pages/Index.razor deleted file mode 100644 index e54d914..0000000 --- a/demo/Blazor.GoogleMap.Client/Pages/Index.razor +++ /dev/null @@ -1,7 +0,0 @@ -@page "/" - -

Hello, world!

- -Welcome to your new app. - - diff --git a/demo/Blazor.GoogleMap.Client/Pages/_Imports.razor b/demo/Blazor.GoogleMap.Client/Pages/_Imports.razor deleted file mode 100644 index 0f24eda..0000000 --- a/demo/Blazor.GoogleMap.Client/Pages/_Imports.razor +++ /dev/null @@ -1 +0,0 @@ -@layout MainLayout diff --git a/demo/Blazor.GoogleMap.Client/Program.cs b/demo/Blazor.GoogleMap.Client/Program.cs deleted file mode 100644 index 95a911d..0000000 --- a/demo/Blazor.GoogleMap.Client/Program.cs +++ /dev/null @@ -1,16 +0,0 @@ -using Microsoft.AspNetCore.Blazor.Hosting; - -namespace Blazor.GoogleMap.Client -{ - public class Program - { - public static void Main(string[] args) - { - CreateHostBuilder(args).Build().Run(); - } - - public static IWebAssemblyHostBuilder CreateHostBuilder(string[] args) => - BlazorWebAssemblyHost.CreateDefaultBuilder() - .UseBlazorStartup(); - } -} diff --git a/demo/Blazor.GoogleMap.Client/Shared/NavMenu.razor b/demo/Blazor.GoogleMap.Client/Shared/NavMenu.razor deleted file mode 100644 index 242dc90..0000000 --- a/demo/Blazor.GoogleMap.Client/Shared/NavMenu.razor +++ /dev/null @@ -1,37 +0,0 @@ - - -
- -
- -@functions { - bool collapseNavMenu = true; - - string NavMenuCssClass => collapseNavMenu ? "collapse" : null; - - void ToggleNavMenu() - { - collapseNavMenu = !collapseNavMenu; - } -} diff --git a/demo/Blazor.GoogleMap.Client/Shared/SurveyPrompt.razor b/demo/Blazor.GoogleMap.Client/Shared/SurveyPrompt.razor deleted file mode 100644 index 1db3c13..0000000 --- a/demo/Blazor.GoogleMap.Client/Shared/SurveyPrompt.razor +++ /dev/null @@ -1,15 +0,0 @@ - - -@functions { - // Demonstrates how a parent component can supply parameters - [Parameter] string Title { get; set; } -} diff --git a/demo/Blazor.GoogleMap.Client/Startup.cs b/demo/Blazor.GoogleMap.Client/Startup.cs deleted file mode 100644 index 5a2114f..0000000 --- a/demo/Blazor.GoogleMap.Client/Startup.cs +++ /dev/null @@ -1,21 +0,0 @@ -using Microsoft.AspNetCore.Components.Builder; -using Microsoft.Extensions.DependencyInjection; - -namespace Blazor.GoogleMap.Client -{ - public class Startup - { - public void ConfigureServices(IServiceCollection services) - { - services.AddGoogleMaps(options => - { - options.ApiKey = "AIzaSyDdjy-3jYU9UvXJLoTPzSyAhMH-kkiK6h4"; - }); - } - - public void Configure(IComponentsApplicationBuilder app) - { - app.AddComponent("app"); - } - } -} diff --git a/demo/Blazor.GoogleMap.Client/wwwroot/index.html b/demo/Blazor.GoogleMap.Client/wwwroot/index.html deleted file mode 100644 index 8e6633b..0000000 --- a/demo/Blazor.GoogleMap.Client/wwwroot/index.html +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - Blazor.GoogleMap - - - - - - Loading... - - - - diff --git a/demo/Blazor.GoogleMap.Server/App.razor b/demo/Blazor.GoogleMap.Server/App.razor new file mode 100644 index 0000000..1c360b7 --- /dev/null +++ b/demo/Blazor.GoogleMap.Server/App.razor @@ -0,0 +1,10 @@ + + + + + + +

Sorry, there's nothing at this address.

+
+
+
diff --git a/demo/Blazor.GoogleMap.Server/Blazor.GoogleMap.Server.csproj b/demo/Blazor.GoogleMap.Server/Blazor.GoogleMap.Server.csproj index 8c68f54..702b6e2 100644 --- a/demo/Blazor.GoogleMap.Server/Blazor.GoogleMap.Server.csproj +++ b/demo/Blazor.GoogleMap.Server/Blazor.GoogleMap.Server.csproj @@ -1,22 +1,25 @@ - + netcoreapp3.0 - - https://dotnet.myget.org/F/aspnetcore-dev/api/v3/index.json; - https://dotnet.myget.org/F/blazor-dev/api/v3/index.json; - - 7.3 - - + + - - + + blazor:js:%(RecursiveDir)%(Filename)%(Extension) + + + blazor:js:%(RecursiveDir)%(Filename)%(Extension) + + + + + diff --git a/demo/Blazor.GoogleMap.Server/Controllers/SampleDataController.cs b/demo/Blazor.GoogleMap.Server/Controllers/SampleDataController.cs deleted file mode 100644 index cabe7f7..0000000 --- a/demo/Blazor.GoogleMap.Server/Controllers/SampleDataController.cs +++ /dev/null @@ -1,30 +0,0 @@ -using Blazor.GoogleMap.Shared; -using Microsoft.AspNetCore.Mvc; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace Blazor.GoogleMap.Server.Controllers -{ - [Route("api/[controller]")] - public class SampleDataController : Controller - { - private static string[] Summaries = new[] - { - "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" - }; - - [HttpGet("[action]")] - public IEnumerable WeatherForecasts() - { - var rng = new Random(); - return Enumerable.Range(1, 5).Select(index => new WeatherForecast - { - Date = DateTime.Now.AddDays(index), - TemperatureC = rng.Next(-20, 55), - Summary = Summaries[rng.Next(Summaries.Length)] - }); - } - } -} diff --git a/demo/Blazor.GoogleMap.Server/Pages/Error.razor b/demo/Blazor.GoogleMap.Server/Pages/Error.razor new file mode 100644 index 0000000..79929d7 --- /dev/null +++ b/demo/Blazor.GoogleMap.Server/Pages/Error.razor @@ -0,0 +1,16 @@ +@page "/error" + + +

Error.

+

An error occurred while processing your request.

+ +

Development Mode

+

+ Swapping to Development environment will display more detailed information about the error that occurred. +

+

+ The Development environment shouldn't be enabled for deployed applications. + It can result in displaying sensitive information from exceptions to end users. + For local debugging, enable the Development environment by setting the ASPNETCORE_ENVIRONMENT environment variable to Development + and restarting the app. +

\ No newline at end of file diff --git a/demo/Blazor.GoogleMap.Client/Pages/Map.razor b/demo/Blazor.GoogleMap.Server/Pages/Index.razor similarity index 53% rename from demo/Blazor.GoogleMap.Client/Pages/Map.razor rename to demo/Blazor.GoogleMap.Server/Pages/Index.razor index a693914..8a3eabd 100644 --- a/demo/Blazor.GoogleMap.Client/Pages/Map.razor +++ b/demo/Blazor.GoogleMap.Server/Pages/Index.razor @@ -1,10 +1,15 @@ -@page "/map" +@page "/" + +

Hello, world!

+ @inject MarkerCollectionFactory MarkerCollectionFactory; -@inject InfoWindow InfoWindow; +@inject InfoWindow InfoWindow +@inject IJSRuntime JsRuntime; + +

This is a Google Map component demo for a Blazor App.

-

Google Map

+ -

Infowindow 1

@@ -13,8 +18,8 @@

@selectedMarker.Id

} + -
@@ -26,8 +31,8 @@

@selectedMarker.Id

} + - @@ -36,43 +41,33 @@ IMarkerCollection markers; Marker selectedMarker; - protected override void OnInit() + + protected async override Task OnInitializedAsync() { - base.OnInit(); + await base.OnInitializedAsync(); markers = MarkerCollectionFactory.Create(); } - void MapOnClick(MouseEventArgs mouseEvent) + void MapOnClick(Blazor.GoogleMap.Map.Events.MouseEventArgs mouseEvent) { Console.WriteLine($"Clicked! {mouseEvent.LatLng.Lat}, {mouseEvent.LatLng.Lng}"); markers.Add(new MarkerOptions(mouseEvent.LatLng) { Title = $"Test {DateTime.Now}", AssociatedInfoWindowId = markers.Count % 2 == 0 ? "infoWindow" : "infoWindowSecond", - Animation = MarkerAnimation.Drop, - Draggable = true, - //Label = new MarkerLabel("A"), - //Icon = $"{GoogleMapConstants.IconBase}parking_lot_maps.png" - OnMarkerClick = EventCallback.Factory.Create(this, MarkerClick), - OnDragEnd = EventCallback.Factory.Create(this, MarkerDragEnd), - OnMarkerRightClick = EventCallback.Factory.Create(this, MarkerRightClick), + OnMarkerClick = EventCallback.Factory.Create(this, MarkerClick) }); } - void MapOnDoubleClick(MouseEventArgs mouseEvent) + void MapOnDoubleClick(Blazor.GoogleMap.Map.Events.MouseEventArgs mouseEvent) { Console.WriteLine($"DoubleClicked! {mouseEvent.LatLng.Lat}, {mouseEvent.LatLng.Lng}"); } - Task MarkerClick(Marker marker) + void MarkerClick(Marker marker) { selectedMarker = marker; Console.WriteLine(marker.Options.Title); - - return Task.WhenAll( - selectedMarker.SetOpacity(0.5), - selectedMarker.SetIcon($"{GoogleMapConstants.IconBase}parking_lot_maps.png") - ); } async Task RemoveMarker() @@ -83,14 +78,4 @@ Console.WriteLine($"Marker removed: {removedResult}"); } } - - void MarkerDragEnd(Marker marker) - { - Console.WriteLine($"MarkerDragEnd: {marker.Options.Position}"); - } - - void MarkerRightClick(Marker marker) - { - marker.OpenInfoWindow(new MarkupString("

Simple static content

")); - } } diff --git a/demo/Blazor.GoogleMap.Server/Pages/_Host.cshtml b/demo/Blazor.GoogleMap.Server/Pages/_Host.cshtml new file mode 100644 index 0000000..f9ec138 --- /dev/null +++ b/demo/Blazor.GoogleMap.Server/Pages/_Host.cshtml @@ -0,0 +1,25 @@ +@page "/" +@namespace Blazor.GoogleMap.Server.Pages +@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers + + + + + + + Blazor.GoogleMap.Server + + + + + + + @(await Html.RenderComponentAsync(RenderMode.ServerPrerendered)) + + + + + + + + diff --git a/demo/Blazor.GoogleMap.Server/Program.cs b/demo/Blazor.GoogleMap.Server/Program.cs index 3dacac5..3521b96 100644 --- a/demo/Blazor.GoogleMap.Server/Program.cs +++ b/demo/Blazor.GoogleMap.Server/Program.cs @@ -1,6 +1,13 @@ -using Microsoft.AspNetCore; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; namespace Blazor.GoogleMap.Server { @@ -8,15 +15,14 @@ public class Program { public static void Main(string[] args) { - BuildWebHost(args).Run(); + CreateHostBuilder(args).Build().Run(); } - public static IWebHost BuildWebHost(string[] args) => - WebHost.CreateDefaultBuilder(args) - .UseConfiguration(new ConfigurationBuilder() - .AddCommandLine(args) - .Build()) - .UseStartup() - .Build(); + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + }); } } diff --git a/demo/Blazor.GoogleMap.Client/Shared/MainLayout.razor b/demo/Blazor.GoogleMap.Server/Shared/MainLayout.razor similarity index 70% rename from demo/Blazor.GoogleMap.Client/Shared/MainLayout.razor rename to demo/Blazor.GoogleMap.Server/Shared/MainLayout.razor index 0f4e22a..9b5407d 100644 --- a/demo/Blazor.GoogleMap.Client/Shared/MainLayout.razor +++ b/demo/Blazor.GoogleMap.Server/Shared/MainLayout.razor @@ -6,7 +6,7 @@
- About + About
diff --git a/demo/Blazor.GoogleMap.Server/Shared/NavMenu.razor b/demo/Blazor.GoogleMap.Server/Shared/NavMenu.razor new file mode 100644 index 0000000..2127987 --- /dev/null +++ b/demo/Blazor.GoogleMap.Server/Shared/NavMenu.razor @@ -0,0 +1,27 @@ + + +
+ +
+ +@code { + bool collapseNavMenu = true; + + string NavMenuCssClass => collapseNavMenu ? "collapse" : null; + + void ToggleNavMenu() + { + collapseNavMenu = !collapseNavMenu; + } +} diff --git a/demo/Blazor.GoogleMap.Server/Startup.cs b/demo/Blazor.GoogleMap.Server/Startup.cs index 4d8bb09..0343329 100644 --- a/demo/Blazor.GoogleMap.Server/Startup.cs +++ b/demo/Blazor.GoogleMap.Server/Startup.cs @@ -1,46 +1,57 @@ using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.ResponseCompression; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; -using Newtonsoft.Json.Serialization; -using System.Linq; namespace Blazor.GoogleMap.Server { public class Startup { + public Startup(IConfiguration configuration) + { + Configuration = configuration; + } + + public IConfiguration Configuration { get; } + // This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { - services.AddMvc().AddNewtonsoftJson(); - services.AddResponseCompression(opts => + services.AddRazorPages(); + services.AddServerSideBlazor().AddCircuitOptions(options => options.DetailedErrors = true); + + services.AddGoogleMaps(options => { - opts.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat( - new[] { "application/octet-stream" }); + options.ApiKey = Configuration["GoogleAPI"]; }); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { - app.UseResponseCompression(); - if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); - app.UseBlazorDebugging(); + } + else + { + app.UseExceptionHandler("/Error"); + // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. + app.UseHsts(); } + app.UseHttpsRedirection(); + app.UseStaticFiles(); + app.UseRouting(); app.UseEndpoints(endpoints => { - endpoints.MapDefaultControllerRoute(); + endpoints.MapBlazorHub(); + endpoints.MapFallbackToPage("/_Host"); }); - - app.UseBlazor(); } } } diff --git a/demo/Blazor.GoogleMap.Client/_Imports.razor b/demo/Blazor.GoogleMap.Server/_Imports.razor similarity index 56% rename from demo/Blazor.GoogleMap.Client/_Imports.razor rename to demo/Blazor.GoogleMap.Server/_Imports.razor index b6b2538..0310bd2 100644 --- a/demo/Blazor.GoogleMap.Client/_Imports.razor +++ b/demo/Blazor.GoogleMap.Server/_Imports.razor @@ -1,10 +1,12 @@ -@using System.Net.Http +@using System.Net.Http +@using Microsoft.AspNetCore.Authorization +@using Microsoft.AspNetCore.Components.Authorization @using Microsoft.AspNetCore.Components.Forms -@using Microsoft.AspNetCore.Components.Layouts @using Microsoft.AspNetCore.Components.Routing +@using Microsoft.AspNetCore.Components.Web @using Microsoft.JSInterop -@using Blazor.GoogleMap.Client -@using Blazor.GoogleMap.Client.Shared +@using Blazor.GoogleMap.Server +@using Blazor.GoogleMap.Server.Shared @using Blazor.GoogleMap.Components; @using Blazor.GoogleMap.Map.Events; @using Blazor.GoogleMap.Map.Markers; diff --git a/demo/Blazor.GoogleMap.Server/appsettings.json b/demo/Blazor.GoogleMap.Server/appsettings.json new file mode 100644 index 0000000..ff17aef --- /dev/null +++ b/demo/Blazor.GoogleMap.Server/appsettings.json @@ -0,0 +1,11 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "AllowedHosts": "*", + "GoogleAPI": "" +} diff --git a/demo/Blazor.GoogleMap.Server/wwwroot/GoogleMapInterop.js b/demo/Blazor.GoogleMap.Server/wwwroot/GoogleMapInterop.js new file mode 100644 index 0000000..9a1db77 --- /dev/null +++ b/demo/Blazor.GoogleMap.Server/wwwroot/GoogleMapInterop.js @@ -0,0 +1,75 @@ +// This file is to show how a library package may provide JavaScript interop features +// wrapped in a .NET API + +window.blazorGoogleMap = { + infoWindowContentNodes: [], + + initMapCallback: function () { + var currentThis = this === window + ? this.blazorGoogleMap + : this; + + currentThis.map = new google.maps.Map(document.getElementById('map'), currentThis.initialMapOptions); + currentThis.infoWindow = new google.maps.InfoWindow(); + + if (currentThis.eventHandlersInvoker === undefined) { + return false; + } + + currentThis.initDotnetCallback.invokeMethodAsync('OnInitFinished'); + currentThis.eventHandlersInvoker.invokeMethodAsync('GetMouseEvents') + .then((events) => { + events.map(eventName => { + currentThis.map.addListener(eventName, + (function (e) { + return this.eventHandlersInvoker + .invokeMethodAsync('InvokeMouseEvent', eventName, e); + }).bind(currentThis) + ); + }); + }); + + return true; + }, + + initMap: function (initialMapOptions) { + blazorGoogleMap.initialMapOptions = initialMapOptions; + }, + + openInfoWindow: function (id, positionableObject, htmlContent) { + var content = {}; + if (htmlContent !== undefined) { + content = htmlContent; + } else { + var nodeContent = blazorGoogleMap.infoWindowContentNodes.find(function (n) { + return n.id === id; + }); + + if (nodeContent === undefined) { + var node = document.getElementById(id); + node.removeAttribute("style"); + blazorGoogleMap.infoWindowContentNodes.push({ id: id, node: node }); + content = node; + } else { + content = nodeContent.node; + } + } + + blazorGoogleMap.infoWindow.setContent(content); + + var marker = blazorGoogleMap.markersModule.findMarker(positionableObject.id); + if (marker !== undefined) { + blazorGoogleMap.infoWindow.open(blazorGoogleMap.map, marker); + } else { + blazorGoogleMap.infoWindow.setPosition(positionableObject.position); + blazorGoogleMap.infoWindow.open(blazorGoogleMap.map); + } + }, + registerEventInvokers: function (eventHandlersInvoker, initDotnetCallback) { + if (blazorGoogleMap) { + + blazorGoogleMap.eventHandlersInvoker = eventHandlersInvoker; + blazorGoogleMap.initDotnetCallback = initDotnetCallback; + } + } +}; diff --git a/demo/Blazor.GoogleMap.Server/wwwroot/GoogleMapMarkerInterop.js b/demo/Blazor.GoogleMap.Server/wwwroot/GoogleMapMarkerInterop.js new file mode 100644 index 0000000..687fd83 --- /dev/null +++ b/demo/Blazor.GoogleMap.Server/wwwroot/GoogleMapMarkerInterop.js @@ -0,0 +1,109 @@ +window.blazorGoogleMap = window.blazorGoogleMap || {}; +window.blazorGoogleMap.markersModule = { + markers: [], + + addMarker: function (markerRef, marker, markerId) { + var that = blazorGoogleMap.markersModule; + var mapMarker = new google.maps.Marker({ + map: blazorGoogleMap.map, + position: marker.position, + title: marker.title, + animation: marker.animation, + label: marker.label, + icon: marker.icon, + opacity: marker.opacity, + draggable: marker.draggable, + clickable: marker.clickable, + cursor: marker.cursor + }); + + mapMarker.addListener('dragend', function (e) { + return markerRef.invokeMethodAsync('OnDragEndHandle', e); + }); + + if (marker.onClick !== null) { + mapMarker.addListener('click', function () { + return markerRef.invokeMethodAsync('OnClickHandle'); + }); + } + + if (marker.onMarkerRightClick !== null) { + mapMarker.addListener('rightclick', function () { + return markerRef.invokeMethodAsync('OnRightClickHandle'); + }); + } + + if (marker.onMarkerDblClick !== null) { + mapMarker.addListener('dblclick', function () { + return markerRef.invokeMethodAsync('OnDoubleClickHandle'); + }); + } + + mapMarker.id = markerId; + that.markers.push({ mapMarker: mapMarker, markerRef: markerRef }); + + return true; + }, + + removeMarker: function (markerId) { + var markerPair = blazorGoogleMap.markersModule.markers.find(function (m) { + return m.mapMarker.id === markerId; + }); + + if (markerPair === undefined) { + return false; + } + + markerPair.mapMarker.setMap(null); + const filteredMarkers = blazorGoogleMap.markersModule.markers.filter( + function (marker) { + return marker.id !== markerId; + } + ); + + var removeResult = filteredMarkers.length === blazorGoogleMap.markersModule.markers.length - 1; + blazorGoogleMap.markersModule.markers = filteredMarkers; + + return removeResult; + }, + + setAnimation: function (markerId, animation) { + var marker =blazorGoogleMap.markersModule.findMarker(markerId); + if (marker !== undefined) { + marker.setAnimation(animation); + } + }, + + setIcon: function (markerId, icon) { + var marker = blazorGoogleMap.markersModule.findMarker(markerId); + if (marker !== undefined) { + marker.setIcon(icon); + } + }, + + setOpacity: function (markerId, opacity) { + var marker = blazorGoogleMap.markersModule.findMarker(markerId); + if (marker !== undefined) { + marker.setOpacity(opacity); + } + }, + + setVisibility: function (markerId, visible) { + var marker = blazorGoogleMap.markersModule.findMarker(markerId); + if (marker !== undefined) { + marker.setVisible(visible); + } + }, + + findMarker: function (markerId) { + var that = this === window + ? blazorGoogleMap.markersModule + : this; + + var marker = that.markers.find(function (m) { + return m.mapMarker.id === markerId; + }); + + return marker.mapMarker; + } +}; \ No newline at end of file diff --git a/demo/Blazor.GoogleMap.Client/wwwroot/css/bootstrap/bootstrap.min.css b/demo/Blazor.GoogleMap.Server/wwwroot/css/bootstrap/bootstrap.min.css similarity index 100% rename from demo/Blazor.GoogleMap.Client/wwwroot/css/bootstrap/bootstrap.min.css rename to demo/Blazor.GoogleMap.Server/wwwroot/css/bootstrap/bootstrap.min.css diff --git a/demo/Blazor.GoogleMap.Client/wwwroot/css/bootstrap/bootstrap.min.css.map b/demo/Blazor.GoogleMap.Server/wwwroot/css/bootstrap/bootstrap.min.css.map similarity index 100% rename from demo/Blazor.GoogleMap.Client/wwwroot/css/bootstrap/bootstrap.min.css.map rename to demo/Blazor.GoogleMap.Server/wwwroot/css/bootstrap/bootstrap.min.css.map diff --git a/demo/Blazor.GoogleMap.Client/wwwroot/css/open-iconic/FONT-LICENSE b/demo/Blazor.GoogleMap.Server/wwwroot/css/open-iconic/FONT-LICENSE similarity index 100% rename from demo/Blazor.GoogleMap.Client/wwwroot/css/open-iconic/FONT-LICENSE rename to demo/Blazor.GoogleMap.Server/wwwroot/css/open-iconic/FONT-LICENSE diff --git a/demo/Blazor.GoogleMap.Client/wwwroot/css/open-iconic/ICON-LICENSE b/demo/Blazor.GoogleMap.Server/wwwroot/css/open-iconic/ICON-LICENSE similarity index 100% rename from demo/Blazor.GoogleMap.Client/wwwroot/css/open-iconic/ICON-LICENSE rename to demo/Blazor.GoogleMap.Server/wwwroot/css/open-iconic/ICON-LICENSE diff --git a/demo/Blazor.GoogleMap.Client/wwwroot/css/open-iconic/README.md b/demo/Blazor.GoogleMap.Server/wwwroot/css/open-iconic/README.md similarity index 100% rename from demo/Blazor.GoogleMap.Client/wwwroot/css/open-iconic/README.md rename to demo/Blazor.GoogleMap.Server/wwwroot/css/open-iconic/README.md diff --git a/demo/Blazor.GoogleMap.Client/wwwroot/css/open-iconic/font/css/open-iconic-bootstrap.min.css b/demo/Blazor.GoogleMap.Server/wwwroot/css/open-iconic/font/css/open-iconic-bootstrap.min.css similarity index 100% rename from demo/Blazor.GoogleMap.Client/wwwroot/css/open-iconic/font/css/open-iconic-bootstrap.min.css rename to demo/Blazor.GoogleMap.Server/wwwroot/css/open-iconic/font/css/open-iconic-bootstrap.min.css diff --git a/demo/Blazor.GoogleMap.Client/wwwroot/css/open-iconic/font/fonts/open-iconic.eot b/demo/Blazor.GoogleMap.Server/wwwroot/css/open-iconic/font/fonts/open-iconic.eot similarity index 100% rename from demo/Blazor.GoogleMap.Client/wwwroot/css/open-iconic/font/fonts/open-iconic.eot rename to demo/Blazor.GoogleMap.Server/wwwroot/css/open-iconic/font/fonts/open-iconic.eot diff --git a/demo/Blazor.GoogleMap.Client/wwwroot/css/open-iconic/font/fonts/open-iconic.otf b/demo/Blazor.GoogleMap.Server/wwwroot/css/open-iconic/font/fonts/open-iconic.otf similarity index 100% rename from demo/Blazor.GoogleMap.Client/wwwroot/css/open-iconic/font/fonts/open-iconic.otf rename to demo/Blazor.GoogleMap.Server/wwwroot/css/open-iconic/font/fonts/open-iconic.otf diff --git a/demo/Blazor.GoogleMap.Client/wwwroot/css/open-iconic/font/fonts/open-iconic.svg b/demo/Blazor.GoogleMap.Server/wwwroot/css/open-iconic/font/fonts/open-iconic.svg similarity index 100% rename from demo/Blazor.GoogleMap.Client/wwwroot/css/open-iconic/font/fonts/open-iconic.svg rename to demo/Blazor.GoogleMap.Server/wwwroot/css/open-iconic/font/fonts/open-iconic.svg diff --git a/demo/Blazor.GoogleMap.Client/wwwroot/css/open-iconic/font/fonts/open-iconic.ttf b/demo/Blazor.GoogleMap.Server/wwwroot/css/open-iconic/font/fonts/open-iconic.ttf similarity index 100% rename from demo/Blazor.GoogleMap.Client/wwwroot/css/open-iconic/font/fonts/open-iconic.ttf rename to demo/Blazor.GoogleMap.Server/wwwroot/css/open-iconic/font/fonts/open-iconic.ttf diff --git a/demo/Blazor.GoogleMap.Client/wwwroot/css/open-iconic/font/fonts/open-iconic.woff b/demo/Blazor.GoogleMap.Server/wwwroot/css/open-iconic/font/fonts/open-iconic.woff similarity index 100% rename from demo/Blazor.GoogleMap.Client/wwwroot/css/open-iconic/font/fonts/open-iconic.woff rename to demo/Blazor.GoogleMap.Server/wwwroot/css/open-iconic/font/fonts/open-iconic.woff diff --git a/demo/Blazor.GoogleMap.Client/wwwroot/css/site.css b/demo/Blazor.GoogleMap.Server/wwwroot/css/site.css similarity index 56% rename from demo/Blazor.GoogleMap.Client/wwwroot/css/site.css rename to demo/Blazor.GoogleMap.Server/wwwroot/css/site.css index fa2c7f1..c127ff7 100644 --- a/demo/Blazor.GoogleMap.Client/wwwroot/css/site.css +++ b/demo/Blazor.GoogleMap.Server/wwwroot/css/site.css @@ -1,9 +1,19 @@ -@import url('open-iconic/font/css/open-iconic-bootstrap.min.css'); +@import url('open-iconic/font/css/open-iconic-bootstrap.min.css'); html, body { font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; } +a, .btn-link { + color: #0366d6; +} + +.btn-primary { + color: #fff; + background-color: #1b6ec2; + border-color: #1861ac; +} + app { position: relative; display: flex; @@ -20,61 +30,66 @@ app { flex: 1; } - .main .top-row { - background-color: #e6e6e6; - border-bottom: 1px solid #d6d5d5; - } +.main .top-row { + background-color: #f7f7f7; + border-bottom: 1px solid #d6d5d5; + justify-content: flex-end; +} + +.main .top-row > a { + margin-left: 1.5rem; +} .sidebar { background-image: linear-gradient(180deg, rgb(5, 39, 103) 0%, #3a0647 70%); } - .sidebar .top-row { - background-color: rgba(0,0,0,0.4); - } +.sidebar .top-row { + background-color: rgba(0,0,0,0.4); +} - .sidebar .navbar-brand { - font-size: 1.1rem; - } +.sidebar .navbar-brand { + font-size: 1.1rem; +} - .sidebar .oi { - width: 2rem; - font-size: 1.1rem; - vertical-align: text-top; - top: -2px; - } +.sidebar .oi { + width: 2rem; + font-size: 1.1rem; + vertical-align: text-top; + top: -2px; +} .nav-item { font-size: 0.9rem; padding-bottom: 0.5rem; } - .nav-item:first-of-type { - padding-top: 1rem; - } +.nav-item:first-of-type { + padding-top: 1rem; +} - .nav-item:last-of-type { - padding-bottom: 1rem; - } +.nav-item:last-of-type { + padding-bottom: 1rem; +} - .nav-item a { - color: #d7d7d7; - border-radius: 4px; - height: 3rem; - display: flex; - align-items: center; - line-height: 3rem; - } +.nav-item a { + color: #d7d7d7; + border-radius: 4px; + height: 3rem; + display: flex; + align-items: center; + line-height: 3rem; +} - .nav-item a.active { - background-color: rgba(255,255,255,0.25); - color: white; - } +.nav-item a.active { + background-color: rgba(255,255,255,0.25); + color: white; +} - .nav-item a:hover { - background-color: rgba(255,255,255,0.1); - color: white; - } +.nav-item a:hover { + background-color: rgba(255,255,255,0.1); + color: white; +} .content { padding-top: 1.1rem; diff --git a/demo/Blazor.GoogleMap.Server/wwwroot/favicon.ico b/demo/Blazor.GoogleMap.Server/wwwroot/favicon.ico new file mode 100644 index 0000000..a3a7999 Binary files /dev/null and b/demo/Blazor.GoogleMap.Server/wwwroot/favicon.ico differ diff --git a/demo/Blazor.GoogleMap.Shared/Blazor.GoogleMap.Shared.csproj b/demo/Blazor.GoogleMap.Shared/Blazor.GoogleMap.Shared.csproj deleted file mode 100644 index 2a77f0c..0000000 --- a/demo/Blazor.GoogleMap.Shared/Blazor.GoogleMap.Shared.csproj +++ /dev/null @@ -1,8 +0,0 @@ - - - - netstandard2.0 - 7.3 - - - diff --git a/demo/Blazor.GoogleMap.Shared/WeatherForecast.cs b/demo/Blazor.GoogleMap.Shared/WeatherForecast.cs deleted file mode 100644 index ef56edb..0000000 --- a/demo/Blazor.GoogleMap.Shared/WeatherForecast.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace Blazor.GoogleMap.Shared -{ - public class WeatherForecast - { - public DateTime Date { get; set; } - - public int TemperatureC { get; set; } - - public string Summary { get; set; } - - public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); - } -} diff --git a/src/Blazor.GoogleMap/Blazor.GoogleMap.csproj b/src/Blazor.GoogleMap/Blazor.GoogleMap.csproj index 519d1a5..6b8f829 100644 --- a/src/Blazor.GoogleMap/Blazor.GoogleMap.csproj +++ b/src/Blazor.GoogleMap/Blazor.GoogleMap.csproj @@ -1,7 +1,7 @@  - netstandard2.0 + netcoreapp3.0 Library true false @@ -34,11 +34,8 @@ - - - - + diff --git a/src/Blazor.GoogleMap/Components/GoogleMap.cs b/src/Blazor.GoogleMap/Components/GoogleMap.cs index 335be75..aff9e0d 100644 --- a/src/Blazor.GoogleMap/Components/GoogleMap.cs +++ b/src/Blazor.GoogleMap/Components/GoogleMap.cs @@ -2,7 +2,9 @@ using Blazor.GoogleMap.Map; using Blazor.GoogleMap.Map.Events; using Microsoft.AspNetCore.Components; +using Microsoft.AspNetCore.Components.Rendering; using Microsoft.AspNetCore.Components.RenderTree; +using System; using System.Threading.Tasks; namespace Blazor.GoogleMap.Components @@ -18,15 +20,15 @@ public class GoogleMap : ComponentBase [Inject] IMouseEventsInovkable MouseEventsInovkable { get; set; } - [Parameter] string Id { get; set; } = "map"; + [Parameter] public string Id { get; set; } = "map"; - [Parameter] string CssClasses { get; set; } = "map"; + [Parameter] public string CssClasses { get; set; } = "map"; - [Parameter] InitialMapOptions InitialMapOptions { get; set; } = new InitialMapOptions(); + [Parameter] public InitialMapOptions InitialMapOptions { get; set; } = new InitialMapOptions(); - [Parameter] EventCallback OnClick { get; set; } + [Parameter] public EventCallback OnClick { get; set; } - [Parameter] EventCallback OnDoubleClick { get; set; } + [Parameter] public EventCallback OnDoubleClick { get; set; } public GoogleMap() { @@ -37,45 +39,48 @@ protected override void BuildRenderTree(RenderTreeBuilder builder) base.BuildRenderTree(builder); var internalBuilder = new BlazorRendererTreeBuilder(builder); + if(string.IsNullOrEmpty(GoogleMapOptions.Width) || string.IsNullOrEmpty(GoogleMapOptions.Height)) + { + throw new ArgumentNullException("Map width and height must have some values."); + } + internalBuilder .OpenElement("div") + .AddAttribute("style", $"width: {GoogleMapOptions.Width}; height: {GoogleMapOptions.Height}") .AddAttribute("id", Id) .AddAttribute("class", CssClasses) .OpenElement("script") .AddAttribute("async", "") .AddAttribute("defer", "") - .AddAttribute("src", GoogleMapOptions.MapJsWasIncluded - ? string.Empty - : $"https://maps.googleapis.com/maps/api/js?key={GoogleMapOptions.ApiKey}&callback=blazorGoogleMap.initMapCallback") + .AddAttribute("src", !string.IsNullOrEmpty(GoogleMapOptions.ApiKey) + ? $"https://maps.googleapis.com/maps/api/js?key={GoogleMapOptions.ApiKey}&callback=blazorGoogleMap.initMapCallback" + : string.Empty + ) .CloseElement() .CloseElement(); } - protected override async Task OnInitAsync() + protected async override Task OnAfterRenderAsync(bool firstRender) { + MouseEventsInovkable - .RegisterCallback(MouseEvent.Click, OnClick) - .RegisterCallback(MouseEvent.DblClick, OnDoubleClick); + .RegisterCallback(MouseEvent.Click, OnClick) + .RegisterCallback(MouseEvent.DblClick, OnDoubleClick); - await GoogleMapInterop.RegisterCallbacks(new Map.InitMapCallback(MapInitialized)); + await GoogleMapInterop.RegisterCallbacks(new InitMapCallback(MapInitialized)); await GoogleMapInterop.InitMap(InitialMapOptions); - } - protected override Task OnAfterRenderAsync() - { - if (GoogleMapOptions.MapJsWasIncluded && !mapWasInitialized) + if (!mapWasInitialized && firstRender) { - return GoogleMapInterop.ExecuteInitMapCallback(); + await GoogleMapInterop.ExecuteInitMapCallback(); + mapWasInitialized = true; } - - return Task.CompletedTask; } private void MapInitialized() { - GoogleMapOptions.MapJsWasIncluded = true; - mapWasInitialized = true; + //TODO: Do some extra operations if needed when map is initialized } } } diff --git a/src/Blazor.GoogleMap/Components/GoogleMapInfoWindow.cs b/src/Blazor.GoogleMap/Components/GoogleMapInfoWindow.cs index 4eccf08..8e247e4 100644 --- a/src/Blazor.GoogleMap/Components/GoogleMapInfoWindow.cs +++ b/src/Blazor.GoogleMap/Components/GoogleMapInfoWindow.cs @@ -1,5 +1,6 @@ using Blazor.GoogleMap.Components.Rendering; using Microsoft.AspNetCore.Components; +using Microsoft.AspNetCore.Components.Rendering; using Microsoft.AspNetCore.Components.RenderTree; using System; @@ -8,9 +9,9 @@ namespace Blazor.GoogleMap.Components [Route("/googlemapinfowindow")] public class GoogleMapInfoWindow : ComponentBase { - [Parameter] RenderFragment ChildContent { get; set; } + [Parameter] public RenderFragment ChildContent { get; set; } - [Parameter] string Id { get; set; } + [Parameter] public string Id { get; set; } protected override void BuildRenderTree(RenderTreeBuilder builder) { diff --git a/src/Blazor.GoogleMap/Components/Rendering/BlazorRendererTreeBuilder.cs b/src/Blazor.GoogleMap/Components/Rendering/BlazorRendererTreeBuilder.cs index 497c0ee..0d4708a 100644 --- a/src/Blazor.GoogleMap/Components/Rendering/BlazorRendererTreeBuilder.cs +++ b/src/Blazor.GoogleMap/Components/Rendering/BlazorRendererTreeBuilder.cs @@ -1,5 +1,5 @@ using Microsoft.AspNetCore.Components; -using Microsoft.AspNetCore.Components.RenderTree; +using Microsoft.AspNetCore.Components.Rendering; using System; namespace Blazor.GoogleMap.Components.Rendering @@ -42,7 +42,7 @@ public IRendererTreeBuilder AddAttribute(string name, MulticastDelegate value) return this; } - public IRendererTreeBuilder AddAttribute(string name, Action value) + public IRendererTreeBuilder AddAttribute(string name, Action value) { renderTreeBuilder.AddAttribute(++sequence, name, value); diff --git a/src/Blazor.GoogleMap/Components/Rendering/IRendererTreeBuilder.cs b/src/Blazor.GoogleMap/Components/Rendering/IRendererTreeBuilder.cs index 7200fce..09af4c0 100644 --- a/src/Blazor.GoogleMap/Components/Rendering/IRendererTreeBuilder.cs +++ b/src/Blazor.GoogleMap/Components/Rendering/IRendererTreeBuilder.cs @@ -19,7 +19,7 @@ public interface IRendererTreeBuilder IRendererTreeBuilder AddAttribute(string name, Func value); - IRendererTreeBuilder AddAttribute(string name, Action value); + IRendererTreeBuilder AddAttribute(string name, Action value); IRendererTreeBuilder AddContent(string textContent); diff --git a/src/Blazor.GoogleMap/GoogleMapInterop.cs b/src/Blazor.GoogleMap/GoogleMapInterop.cs index 1ef6b3d..92a4680 100644 --- a/src/Blazor.GoogleMap/GoogleMapInterop.cs +++ b/src/Blazor.GoogleMap/GoogleMapInterop.cs @@ -18,27 +18,29 @@ public GoogleMapInterop(IJSRuntime jSRuntime, IMouseEventsInovkable mouseEventsI this.mouseEventsInovkable = mouseEventsInovkable ?? throw new ArgumentNullException(nameof(mouseEventsInovkable)); } - public Task InitMap(InitialMapOptions initialMapOptions) + public async Task InitMap(InitialMapOptions initialMapOptions) { - return jSRuntime.InvokeAsync("blazorGoogleMap.initMap", initialMapOptions); + + await jSRuntime.InvokeVoidAsync("blazorGoogleMap.initMap", initialMapOptions); } - public Task ExecuteInitMapCallback() + public async Task ExecuteInitMapCallback() { - return jSRuntime.InvokeAsync("blazorGoogleMap.initMapCallback"); + await jSRuntime.InvokeAsync("blazorGoogleMap.initMapCallback"); } - public Task RegisterCallbacks(InitMapCallback initMapCallback) + public async Task RegisterCallbacks(InitMapCallback initMapCallback) { - return jSRuntime.InvokeAsync( - "blazorGoogleMap.registerEventInvokers", - new DotNetObjectRef(mouseEventsInovkable), new DotNetObjectRef(initMapCallback)); + var mouseEvents = DotNetObjectReference.Create(mouseEventsInovkable); + var mapCallBack = DotNetObjectReference.Create(initMapCallback); + + await jSRuntime.InvokeAsync("blazorGoogleMap.registerEventInvokers", mouseEvents,mapCallBack ); + } - public Task CreateInfoWindow(string id, LatLng position) + public async Task CreateInfoWindow(string id, LatLng position) { - return jSRuntime.InvokeAsync( - "blazorGoogleMap.createInfoWindow", id, position); + return await jSRuntime.InvokeAsync("blazorGoogleMap.createInfoWindow", id, position); } } } diff --git a/src/Blazor.GoogleMap/GoogleMapOptions.cs b/src/Blazor.GoogleMap/GoogleMapOptions.cs index 881a6fa..3d04508 100644 --- a/src/Blazor.GoogleMap/GoogleMapOptions.cs +++ b/src/Blazor.GoogleMap/GoogleMapOptions.cs @@ -3,6 +3,9 @@ public class GoogleMapOptions { public string ApiKey { get; set; } + public string Width { get; set; } = "600px"; + public string Height { get; set; } = "600px"; + } internal class GoogleMapRestrictedOptions : GoogleMapOptions diff --git a/src/Blazor.GoogleMap/Map/InfoWindows/IInfoWindowHandle.cs b/src/Blazor.GoogleMap/Map/InfoWindows/IInfoWindowHandle.cs index 3a47495..47a93c2 100644 --- a/src/Blazor.GoogleMap/Map/InfoWindows/IInfoWindowHandle.cs +++ b/src/Blazor.GoogleMap/Map/InfoWindows/IInfoWindowHandle.cs @@ -5,8 +5,8 @@ namespace Blazor.GoogleMap.Map.InfoWindows { public interface IInfoWindowHandle { - Task Open(string infoWindowId, ILocationable locationable); + Task Open(string infoWindowId, ILocationable locationable); - Task Open(MarkupString htmlContent, ILocationable locationable); + Task Open(MarkupString htmlContent, ILocationable locationable); } } diff --git a/src/Blazor.GoogleMap/Map/InfoWindows/InfoWindow.cs b/src/Blazor.GoogleMap/Map/InfoWindows/InfoWindow.cs index 86c25b0..6ed0a30 100644 --- a/src/Blazor.GoogleMap/Map/InfoWindows/InfoWindow.cs +++ b/src/Blazor.GoogleMap/Map/InfoWindows/InfoWindow.cs @@ -11,15 +11,15 @@ public InfoWindow(IJSRuntime jSRuntime) { } - public Task Open(string infoWindowId, ILocationable locationable = null) + public async Task Open(string infoWindowId, ILocationable locationable = null) { - return jSRuntime.InvokeAsync( + return await jSRuntime.InvokeAsync( "blazorGoogleMap.openInfoWindow", infoWindowId, locationable); } - public Task Open(MarkupString htmlContent, ILocationable locationable) + public async Task Open(MarkupString htmlContent, ILocationable locationable) { - return jSRuntime.InvokeAsync( + return await jSRuntime.InvokeAsync( "blazorGoogleMap.openInfoWindow", string.Empty, locationable, htmlContent.ToString()); } } diff --git a/src/Blazor.GoogleMap/Map/Markers/Marker.cs b/src/Blazor.GoogleMap/Map/Markers/Marker.cs index c671e15..fd69db6 100644 --- a/src/Blazor.GoogleMap/Map/Markers/Marker.cs +++ b/src/Blazor.GoogleMap/Map/Markers/Marker.cs @@ -83,21 +83,21 @@ await jSRuntime.InvokeAsync( "blazorGoogleMap.markersModule.setAnimation", Id, markerAnimation); } - public Task SetIcon(string iconUrl) + public async Task SetIcon(string iconUrl) { Options.Icon = iconUrl; - return jSRuntime.InvokeAsync( + return await jSRuntime.InvokeAsync( "blazorGoogleMap.markersModule.setIcon", Id, iconUrl); } - public Task SetOpacity(double opacity) + public async Task> SetOpacity(double opacity) { Options.Opacity = opacity; return jSRuntime.InvokeAsync( "blazorGoogleMap.markersModule.setOpacity", Id, opacity); } - public Task SetVisibility(bool visible) + public async Task> SetVisibility(bool visible) { Options.Visible = visible; return jSRuntime.InvokeAsync( diff --git a/src/Blazor.GoogleMap/Map/Markers/MarkerCollection.cs b/src/Blazor.GoogleMap/Map/Markers/MarkerCollection.cs index fb83dfa..79c608f 100644 --- a/src/Blazor.GoogleMap/Map/Markers/MarkerCollection.cs +++ b/src/Blazor.GoogleMap/Map/Markers/MarkerCollection.cs @@ -32,7 +32,7 @@ public async Task Add(MarkerOptions markerOptions) await jSRuntime.InvokeAsync( "blazorGoogleMap.markersModule.addMarker", - new DotNetObjectRef(marker), markerOptions, marker.Id); + DotNetObjectReference.Create(marker), markerOptions, marker.Id); markers.Add(marker.Id, marker); diff --git a/src/Blazor.GoogleMap/ServiceCollectionExtensions.cs b/src/Blazor.GoogleMap/ServiceCollectionExtensions.cs index 259bfcc..908f7d2 100644 --- a/src/Blazor.GoogleMap/ServiceCollectionExtensions.cs +++ b/src/Blazor.GoogleMap/ServiceCollectionExtensions.cs @@ -20,11 +20,11 @@ public static IServiceCollection AddGoogleMaps(this IServiceCollection services, configure.Invoke(googleMapOptions); services.AddSingleton(googleMapOptions); - services.AddSingleton(); - services.AddSingleton(); - services.AddSingleton(); - services.AddSingleton(); - services.AddSingleton(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); return services; } diff --git a/src/Blazor.GoogleMap/content/GoogleMapInterop.js b/src/Blazor.GoogleMap/content/GoogleMapInterop.js index 7cc73da..de50d2a 100644 --- a/src/Blazor.GoogleMap/content/GoogleMapInterop.js +++ b/src/Blazor.GoogleMap/content/GoogleMapInterop.js @@ -8,7 +8,6 @@ window.blazorGoogleMap = { var currentThis = this === window ? this.blazorGoogleMap : this; - currentThis.map = new google.maps.Map(document.getElementById('map'), currentThis.initialMapOptions); currentThis.infoWindow = new google.maps.InfoWindow(); @@ -33,7 +32,7 @@ window.blazorGoogleMap = { }, initMap: function (initialMapOptions) { - this.blazorGoogleMap.initialMapOptions = initialMapOptions; + blazorGoogleMap.initialMapOptions = initialMapOptions; }, openInfoWindow: function (id, positionableObject, htmlContent) { @@ -41,33 +40,35 @@ window.blazorGoogleMap = { if (htmlContent !== undefined) { content = htmlContent; } else { - var nodeContent = this.blazorGoogleMap.infoWindowContentNodes.find(function (n) { + var nodeContent = blazorGoogleMap.infoWindowContentNodes.find(function (n) { return n.id === id; }); if (nodeContent === undefined) { var node = document.getElementById(id); node.removeAttribute("style"); - this.blazorGoogleMap.infoWindowContentNodes.push({ id: id, node: node }); + blazorGoogleMap.infoWindowContentNodes.push({ id: id, node: node }); content = node; } else { content = nodeContent.node; } } - - this.blazorGoogleMap.infoWindow.setContent(content); - var marker = this.blazorGoogleMap.markersModule.findMarker(positionableObject.id); + blazorGoogleMap.infoWindow.setContent(content); + + var marker = blazorGoogleMap.markersModule.findMarker(positionableObject.id); if (marker !== undefined) { - this.blazorGoogleMap.infoWindow.open(this.blazorGoogleMap.map, marker); + blazorGoogleMap.infoWindow.open(blazorGoogleMap.map, marker); } else { - this.blazorGoogleMap.infoWindow.setPosition(positionableObject.position); - this.blazorGoogleMap.infoWindow.open(this.blazorGoogleMap.map); + blazorGoogleMap.infoWindow.setPosition(positionableObject.position); + blazorGoogleMap.infoWindow.open(blazorGoogleMap.map); } }, - registerEventInvokers: function (eventHandlersInvoker, initDotnetCallback) { - this.blazorGoogleMap.eventHandlersInvoker = eventHandlersInvoker; - this.blazorGoogleMap.initDotnetCallback = initDotnetCallback; + if (blazorGoogleMap) { + + blazorGoogleMap.eventHandlersInvoker = eventHandlersInvoker; + blazorGoogleMap.initDotnetCallback = initDotnetCallback; + } } }; diff --git a/src/Blazor.GoogleMap/content/GoogleMapMarkerInterop.js b/src/Blazor.GoogleMap/content/GoogleMapMarkerInterop.js index df5d9f7..d981793 100644 --- a/src/Blazor.GoogleMap/content/GoogleMapMarkerInterop.js +++ b/src/Blazor.GoogleMap/content/GoogleMapMarkerInterop.js @@ -3,9 +3,9 @@ window.blazorGoogleMap.markersModule = { markers: [], addMarker: function (markerRef, marker, markerId) { - var that = this.blazorGoogleMap.markersModule; + var that = blazorGoogleMap.markersModule; var mapMarker = new google.maps.Marker({ - map: this.blazorGoogleMap.map, + map: blazorGoogleMap.map, position: marker.position, title: marker.title, animation: marker.animation, @@ -46,7 +46,7 @@ window.blazorGoogleMap.markersModule = { }, removeMarker: function (markerId) { - var markerPair = this.blazorGoogleMap.markersModule.markers.find(function (m) { + var markerPair = blazorGoogleMap.markersModule.markers.find(function (m) { return m.mapMarker.id === markerId; }); @@ -55,41 +55,41 @@ window.blazorGoogleMap.markersModule = { } markerPair.mapMarker.setMap(null); - const filteredMarkers = this.blazorGoogleMap.markersModule.markers.filter( + const filteredMarkers = blazorGoogleMap.markersModule.markers.filter( function (marker) { return marker.id !== markerId; } ); - var removeResult = filteredMarkers.length === this.blazorGoogleMap.markersModule.markers.length - 1; - this.blazorGoogleMap.markersModule.markers = filteredMarkers; + var removeResult = filteredMarkers.length === blazorGoogleMap.markersModule.markers.length - 1; + blazorGoogleMap.markersModule.markers = filteredMarkers; return removeResult; }, setAnimation: function (markerId, animation) { - var marker = this.blazorGoogleMap.markersModule.findMarker(markerId); + var marker = blazorGoogleMap.markersModule.findMarker(markerId); if (marker !== undefined) { marker.setAnimation(animation); } }, setIcon: function (markerId, icon) { - var marker = this.blazorGoogleMap.markersModule.findMarker(markerId); + var marker = blazorGoogleMap.markersModule.findMarker(markerId); if (marker !== undefined) { marker.setIcon(icon); } }, setOpacity: function (markerId, opacity) { - var marker = this.blazorGoogleMap.markersModule.findMarker(markerId); + var marker = blazorGoogleMap.markersModule.findMarker(markerId); if (marker !== undefined) { marker.setOpacity(opacity); } }, setVisibility: function (markerId, visible) { - var marker = this.blazorGoogleMap.markersModule.findMarker(markerId); + var marker = blazorGoogleMap.markersModule.findMarker(markerId); if (marker !== undefined) { marker.setVisible(visible); } @@ -97,7 +97,7 @@ window.blazorGoogleMap.markersModule = { findMarker: function (markerId) { var that = this === window - ? this.blazorGoogleMap.markersModule + ? blazorGoogleMap.markersModule : this; var marker = that.markers.find(function (m) {