BlazorBasics.Maps.Services 1.2.1

dotnet add package BlazorBasics.Maps.Services --version 1.2.1
                    
NuGet\Install-Package BlazorBasics.Maps.Services -Version 1.2.1
                    
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="BlazorBasics.Maps.Services" Version="1.2.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="BlazorBasics.Maps.Services" Version="1.2.1" />
                    
Directory.Packages.props
<PackageReference Include="BlazorBasics.Maps.Services" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add BlazorBasics.Maps.Services --version 1.2.1
                    
#r "nuget: BlazorBasics.Maps.Services, 1.2.1"
                    
#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.
#:package BlazorBasics.Maps.Services@1.2.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=BlazorBasics.Maps.Services&version=1.2.1
                    
Install as a Cake Addin
#tool nuget:?package=BlazorBasics.Maps.Services&version=1.2.1
                    
Install as a Cake Tool

Nuget Nuget

Geolocation Service for Blazor

A lightweight and efficient Blazor service for interacting with the Browser's Geolocation API. This service uses JS Interop with Lazy Loading to ensure that JavaScript resources are only loaded when needed.

Features

  • Lazy Initialization: The JavaScript module is loaded only upon the first method call.
  • Permission Handling: Includes a built-in check to verify if the user has granted geolocation permissions.
  • Memory Management: Implements IAsyncDisposable to properly release JavaScript object references.
  • Robust Error Handling: Internal try-catch blocks prevent JS runtime errors from crashing the .NET process, logging issues to the browser console instead.

Registration

Add the service to your Dependency Injection (DI) container in Program.cs:

builder.Services.AddGeoService();

Usage

Inject the IGeolocationService into your Razor components to retrieve the user's coordinates.

@page "/location"
@inject IGeolocationService GeolocationService

<PageTitle>Location Tracker</PageTitle>

<h1>Geolocation</h1>

@if (currentPoint != null)
{
    <p>Latitude: @currentPoint.Latitude</p>
    <p>Longitude: @currentPoint.Longitude</p>
}

<button class="btn btn-primary" @onclick="GetUserLocation">
    Get My Location
</button>

@code {
    private ILatLong? currentPoint;

    private async Task GetUserLocation()
    {
        bool isGranted = await GeolocationService.GetGeoLocationGrantedAsync();
        
        if (isGranted)
        {
            currentPoint = await GeolocationService.GetPositionAsync();
        }
        else
        {
            // Handle permission denied or prompt user
            Console.WriteLine("Location access denied.");
        }
    }
}

API Reference

Method|Return Type|Description

Method Type Description
GetPositionAsync() ValueTask<ILatLong> Triggers the browser's geolocation prompt and returns the coordinates.
GetGeoLocationGrantedAsync() ValueTask<bool> Checks if the user has already granted geolocation permissions.
DisposeAsync() ValueTask Disposes of the JavaScript module reference.

Developed for BlazorBasics.Maps

You can use in conjuntion with BlazorBasics.Maps.Google and BlazorBasics.Maps.Leaflet or any other application.

Contributing

If you encounter issues or have suggestions for improvements, please submit an issue or pull request to the repository hosting this library.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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.2.1 319 1/16/2026
1.2.0 132 1/5/2026

Update dependencies.