AugusteVN.Google.Maps.Razor 1.5.3

dotnet add package AugusteVN.Google.Maps.Razor --version 1.5.3                
NuGet\Install-Package AugusteVN.Google.Maps.Razor -Version 1.5.3                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AugusteVN.Google.Maps.Razor" Version="1.5.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AugusteVN.Google.Maps.Razor --version 1.5.3                
#r "nuget: AugusteVN.Google.Maps.Razor, 1.5.3"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install AugusteVN.Google.Maps.Razor as a Cake Addin
#addin nuget:?package=AugusteVN.Google.Maps.Razor&version=1.5.3

// Install AugusteVN.Google.Maps.Razor as a Cake Tool
#tool nuget:?package=AugusteVN.Google.Maps.Razor&version=1.5.3                

Google Maps JavaScript API in Razor

The Google Maps JavaScript API encapsulated in Razor components.

Register

appSettings.json

{
  "GoogleMapsConfig": {
    "ApiKey": "xxx",
    "MapId": "4504f8b37365c3d0"
  }
}

Program.cs

builder.Services
    .AddOptions<GoogleMapsConfig>()
    .BindConfiguration(nameof(GoogleMapsConfig));

builder.Services.AddGoogleMaps(builder.Configuration);

Razor Components

The encapsulating components below, can be implemented on Razor pages or components.

<GoogleMap
    ElementId="map"
    Config="@(new GoogleMapsLoadConfig { ApiKey = "***", ... })"
    MapOptions="@(new GoogleMapOptions(ElementId: "map", MapId: "4504f8b37365c3d0"))"
    Center="@(new GoogleMapCoordinate(-74.0, 40.7))"
    Zoom="11"
    OnMapLoaded="() => {}"
    OnMapDetailsChanged="(details) => { Console.WriteLine(details.EventName); }">

    <GoogleMapMarkers Markers="@(new GoogleMapMarkerInfo[] {
        new GoogleMapMarkerInfo(
            Title: "Statue of Liberty",
            Info: new GoogleMapMarkerInfoWindow(HeaderContent: "<h3>Custom HTML</h3>", Content: "<div>Body</div>"),
            Position: new GoogleMapCoordinate(-74.0, 40.7),
            Clickable: true,
            Pin: new GoogleMapMarkerPin(Content: "<div><h3>Custom HTML</h3></div>")
        ),
        new GoogleMapMarkerInfo(
            Title: "Foo Bar",
            Info: new GoogleMapMarkerInfoWindow(IsHeaderDisabled: true, new GoogleMapMarkerInfoWindowOptions(DisableAutoPan: true)),
            Position: new GoogleMapCoordinate(-75.0, 44.1),
            Clickable: true,
            Pin: new GoogleMapMarkerPin(Content: "<span class="bi bi-person"></span>")
        )
    })"/>
</GoogleMap>

Extension Methods

On the JsRuntime.

Task ImportCustomGoogleMapsJsAsync(this IJSRuntime jsRuntime);

ValueTask LoadGoogleMapsApiAsync(this IJSRuntime jsRuntime, GoogleMapsLoadConfig config);

ValueTask InitGoogleMapAsync(this IJSRuntime jsRuntime, GoogleMapOptions mapOptions, GoogleMapCoordinate center, double zoom, GoogleMapControls? controls)

ValueTask SetGoogleMapCenterAsync(this IJSRuntime jsRuntime, GoogleMapCoordinate center);

ValueTask SetGoogleMapZoomAsync(this IJSRuntime jsRuntime, double zoom);

ValueTask SetGoogleMapMarkersAsync(this IJSRuntime jsRuntime, GoogleMapMarkerInfo[] markers);

ValueTask UnsetGoogleMapMarkersAsync(this IJSRuntime jsRuntime);

Usage Example

@inject IJSRuntime JsRuntime

<div id="@ElementId" style="height: 100%;"></div>

@code {
    [Parameter]
    public string ElementId { get; set; } = "map";

    [Parameter]
    public GoogleMapsLoadConfig Config { get; set; }
    
    [Parameter]
    public GoogleMapOptions MapOptions { get; set; }

    [Parameter]
    public GoogleMapCoordinate Center { get; set; }
    
    [Parameter]
    public double Zoom { get; set; }
    
    [Parameter]
    public GoogleMapControls? Controls { get; set; }

    [Parameter]
    public GoogleMapMarkerInfo[] Markers { get; set; }
    
    [Parameter]
    public EventCallback OnMapLoaded { get; set; }

    [Parameter]
    public EventCallback<GoogleMapDetails> OnMapDetailsChanged { get; set; }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            await JsRuntime.ImportCustomGoogleMapsJsAsync();
            await JsRuntime.LoadGoogleMapsApiAsync(Config);
            await JsRuntime.InitGoogleMapAsync(MapOptions, Center, Zoom, Controls);
            await JsRuntime.SetGoogleMapMarkersAsync(Markers);
            await OnMapLoaded.InvokeAsync();
        }
    }
}

To see it in action, watch: Google Maps API JavaScript Tutorial with Blazor C# .NET 8. Overview Google Maps JavaScript API

More reads:

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.5.3 81 11/15/2024
1.5.2 83 11/14/2024
1.5.1 88 11/5/2024
1.5.0 72 11/5/2024
1.4.1 90 11/3/2024
1.3.8 63 11/3/2024
1.3.7 69 11/3/2024
1.3.3 68 11/3/2024
1.3.2 69 11/3/2024
1.3.1 75 11/3/2024
1.3.0 78 11/3/2024
1.2.9 71 10/31/2024
1.2.8 69 10/31/2024
1.2.5 101 10/24/2024
1.2.0 88 10/23/2024
1.1.9 77 10/23/2024
1.1.8 83 10/12/2024
1.1.7 163 10/12/2024
1.1.5 82 10/11/2024
1.0.8 102 10/11/2024 1.0.8 is deprecated because it is no longer maintained.
1.0.7 107 10/11/2024 1.0.7 is deprecated because it is no longer maintained.
1.0.1 107 10/10/2024 1.0.1 is deprecated because it has critical bugs.

Added Center and Zoom as separate parameters.