Persilsoft.Nominatim.Geolocation.Blazor 1.0.8

There is a newer version of this package available.
See the version list below for details.
dotnet add package Persilsoft.Nominatim.Geolocation.Blazor --version 1.0.8                
NuGet\Install-Package Persilsoft.Nominatim.Geolocation.Blazor -Version 1.0.8                
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="Persilsoft.Nominatim.Geolocation.Blazor" Version="1.0.8" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Persilsoft.Nominatim.Geolocation.Blazor --version 1.0.8                
#r "nuget: Persilsoft.Nominatim.Geolocation.Blazor, 1.0.8"                
#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 Persilsoft.Nominatim.Geolocation.Blazor as a Cake Addin
#addin nuget:?package=Persilsoft.Nominatim.Geolocation.Blazor&version=1.0.8

// Install Persilsoft.Nominatim.Geolocation.Blazor as a Cake Tool
#tool nuget:?package=Persilsoft.Nominatim.Geolocation.Blazor&version=1.0.8                

Persilsoft.Nominatim.Geolocation.Blazor

A Geolocation library for Blazor and Razor Components applications.


Example

Get current position
You should register the service that restrieves the user's coordinates:

using ServiceCollectionExtensions;

builder.Services.AddGeolocationService();

Now, you can retrieve the current location of the user.

@page "/geocoder"
@using Persilsoft.Nominatim.Geolocation.Blazor
@inject GeolocationService Geolocation;

<PageTitle>Geolocation Test</PageTitle>

<h1>Geolocation</h1>
<hr />

<button class="btn btn-primary" @onclick=ShowLocation>
    Show location
</button>

<div class="row mt-2">
    <div class="col-md-6">
        <label class="form-label">Latitude:</label>
        <input type="text" class="form-control" @bind=latitude disabled />
    </div>
    <div class="col-md-6">
        <label class="form-label">Longitude:</label>
        <input type="text" class="form-control" @bind=longitude disabled />
    </div>
    <p>@message</p>
</div>

@code {
    private double latitude;
    private double longitude;
    private string message = string.Empty;

    private async Task ShowLocation()
    {
        var position = await Geolocation.GetPosition();
        if (!position.Equals(default))
        {
            latitude = position.Latitude;
            longitude = position.Longitude;
        }
        else
        {
            message = "Location could not be retrieved.";
        }
    }
}

Reverse Geocoding
If you need to obtain the address of the location, you can use the Reverse Geocoding service.
You should register the Geocoding service:

builder.Services.AddNominatimGeocoderService();

Below, you can modify the previous example to obtain the address.

@page "/geocoder"
@using Persilsoft.Nominatim.Geolocation.Blazor
@using Persilsoft.Nominatim.Geolocation.Blazor.Geocoding
@inject GeolocationService Geolocation
@inject IGeocoder GeocoderService

<PageTitle>Geolocation Test</PageTitle>

<h1>Geolocation</h1>
<hr />

<button class="btn btn-primary" @onclick=ShowLocation>
    Show location
</button>

<div class="row mt-2">
    <div class="col-md-6">
        <label class="form-label">Latitude:</label>
        <input type="text" class="form-control" @bind=latitude disabled />
    </div>
    <div class="col-md-6">
        <label class="form-label">Longitude:</label>
        <input type="text" class="form-control" @bind=longitude disabled />
    </div>
    <p>@message</p>
</div>
@if (addressSectionVisible)
{
    <section>
        <button class="btn btn-warning" @onclick="GetAddress">
            Get Address
        </button>
        <textarea class="form-control mt-2" @bind="addressDet" disabled>
        </textarea>
    </section>
}

@code {
    private double latitude;
    private double longitude;
    private string message = string.Empty;

    private bool addressSectionVisible;
    private string addressDet = string.Empty;

    private async Task ShowLocation()
    {
        var position = await Geolocation.GetPosition();
        if (!position.Equals(default))
        {
            latitude = position.Latitude;
            longitude = position.Longitude;
            addressSectionVisible = true;
        }
        else
        {
            message = "Location could not be retrieved.";
            addressSectionVisible = false;
        }
    }

    private async Task GetAddress()
    {
        var address = await GeocoderService.GetGeocodingAddressAsync(latitude, longitude);
        addressDet = address.DisplayAddress;
    }
}

This package makes use of the Nominatim Geocoding API. For more information, you can visit their site at:
https://nominatim.openstreetmap.org/ui/reverse.html

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 (1)

Showing the top 1 NuGet packages that depend on Persilsoft.Nominatim.Geolocation.Blazor:

Package Downloads
Persilsoft.GeolocationMap.Blazor

This package integrates the retrieval of geographical coordinates and addresses provided by the Persilsoft.Nominatim.Geolocation package, with the mapping capabilities offered by the Persilsoft.Leaflet.Blazor package.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.9 157 10/16/2024
1.0.8 311 8/18/2024
1.0.7 291 7/20/2024
1.0.6 276 5/22/2024
1.0.5 355 4/20/2024
1.0.4 111 4/19/2024
1.0.3 108 4/19/2024
1.0.2 105 4/19/2024
1.0.1 105 4/19/2024