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
<PackageReference Include="BlazorBasics.Maps.Services" Version="1.2.1" />
<PackageVersion Include="BlazorBasics.Maps.Services" Version="1.2.1" />
<PackageReference Include="BlazorBasics.Maps.Services" />
paket add BlazorBasics.Maps.Services --version 1.2.1
#r "nuget: BlazorBasics.Maps.Services, 1.2.1"
#:package BlazorBasics.Maps.Services@1.2.1
#addin nuget:?package=BlazorBasics.Maps.Services&version=1.2.1
#tool nuget:?package=BlazorBasics.Maps.Services&version=1.2.1
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
IAsyncDisposableto 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 | Versions 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. |
-
net10.0
- BlazorBasics.Maps.Entities (>= 1.2.0)
- Microsoft.AspNetCore.Components.Web (>= 10.0.2)
-
net9.0
- BlazorBasics.Maps.Entities (>= 1.2.0)
- Microsoft.AspNetCore.Components.Web (>= 9.0.12)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Update dependencies.