Persilsoft.Leaflet.Blazor 1.0.3

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

// Install Persilsoft.Leaflet.Blazor as a Cake Tool
#tool nuget:?package=Persilsoft.Leaflet.Blazor&version=1.0.3                

Leaflet Blazor

A Leaflet service for Blazor and Razor Components applications.


Example

Simple map
You should register the Leaflet services:

using ServiceCollectionExtensions;

builder.Services.AddLeafletServices();

Now, you can the Map component to your page:

@page "/simplemap"

<div class="map-container">
    <Map ZoomLevel=2 />
</div>

Where:
ZoomLevel: Defines the initial zoom (required).

Optionally, you can set the OriginalPoint parameter:

@page "/original-point"

<div class="map-container">
    <Map ZoomLevel=18
         OriginalPoint="originalPoint" />
</div>

@code {
    private readonly LeafletLatLong originalPoint = new(-12.04490, -77.02980);
}

Where:
OriginalPoint: Defines the geographical center of the map.

You can control the dimensions for the map container.

.map-container {
    width: 100%;
    height: 70vh;
}

Set View / Fly to
Set view: Sets the view of the map (geographical center and zoom)
Fly to: Sets the view of the map (geographical center and zoom) performing a smooth pan-zoom animation.

@page "/setview-flyto"

<div class="row mb-2">
    <div class="col-sm-4">
        <label class="form-label">Latitude</label>
        <input @bind=model.Latitude class="form-control" />
    </div>
    <div class="col-sm-4">
        <label class="form-label">Longitude</label>
        <input @bind=model.Longitude class="form-control" />
    </div>
    <div class="col-sm-4">
        <label class="form-label">Zoom level</label>
        <select @bind=model.ZoomLevel class="form-select">
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
            <option value="5">5</option>
            <option value="6">6</option>
            <option value="7">7</option>
            <option value="8">8</option>
            <option value="9">9</option>
            <option value="10">10</option>
            <option value="11">11</option>
            <option value="12">12</option>
            <option value="13">13</option>
            <option value="14">14</option>
            <option value="15">15</option>
            <option value="16">16</option>
            <option value="17">17</option>
            <option value="18">18</option>
            <option value="19">19</option>
        </select>
    </div>
</div>
<div class="text-center mb-2">
    <button class="btn btn-primary" @onclick=ExcuteSetView>Set view</button>
    <button class="btn btn-info" @onclick=ExcuteFlyTo>Fly to</button>
</div>
<div class="map-container">
    <Map ZoomLevel="1"
         @ref=myMap/>
</div>
@code {
    private InputDataModel model = new();
    private Map myMap;

    private async Task ExcuteSetView()
    {
        await myMap.SetView(new LeafletLatLong(model.Latitude, model.Longitude), model.ZoomLevel);
    }

    private async Task ExcuteFlyTo()
    {
        await myMap.FlyTo(new LeafletLatLong(model.Latitude, model.Longitude), model.ZoomLevel);
    }

    class InputDataModel
    {
        public double Latitude { get; set; } = -12.04490;
        public double Longitude { get; set; } = -77.02980;
        public byte ZoomLevel { get; set; } = 18;
    }
}

Markers
You can add markers

@page "/markers"

<div class="row mb-2">
    <div class="col-sm-6">
        <div class="mb-3">
            <label class="form-label">Title:</label>
            <input class="form-control" @bind=title />
        </div>
        <div class="mb-3">
            <label class="form-label">Latitude (-90 - 90):</label>
            <input class="form-control" @bind=latitude />
        </div>
    </div>
    <div class="col-sm-6">
        <div class="mb-3">
            <label class="form-label">Description:</label>
            <input class="form-control" @bind=description />
        </div>
        <div class="mb-3">
            <label class="form-label">Longitude (-180 - 180):</label>
            <input class="form-control" @bind=longitude />
        </div>
    </div>
</div>
<div class="row mb-2">
    <div class="mb-3">
        <button class="btn btn-info" @onclick=AddDefaultMarker>Add default marker</button>
        <button class="btn btn-warning" @onclick=AddCustomMarker>Add custom marker</button>
        <button class="btn btn-success" @onclick=AddDraggableMarker>Add draggable marker</button>
        <button class="btn btn-primary" @onclick=RemoveFirstMarker>Remove first marker</button>
        <button class="btn btn-danger" @onclick=RemoveAllMarkers>Remove all markers</button>
    </div>
    <div>@message</div>
</div>

<div class="map-container">
    <Map OriginalPoint=originalPoint
         ZoomLevel="14"
         OnCreatedMap="OnCreatedMap" />
</div>
@code {
    private LeafletLatLong originalPoint;
    private Map myMap;

    private string title;
    private string description;
    private double latitude;
    private double longitude;

    private int lastIndex;
    private string message;

    protected override void OnInitialized()
    {
        latitude = -12.04490;
        longitude = -77.02980;
        originalPoint = new LeafletLatLong(latitude, longitude);
    }

    private async Task AddDefaultMarker()
    {
        LeafletLatLong Point = new(latitude, longitude);
        lastIndex = await myMap.AddMarker(Point, title, description);
        message = $"Marker with index {lastIndex} added.";
        StateHasChanged();
    }

    private async Task AddCustomMarker()
    {
        LeafletLatLong Point = new(latitude, longitude);
        lastIndex = await myMap.AddMarker(Point, title, description, "img/destination.png");
        message = $"Marker with index {lastIndex} added.";
        StateHasChanged();
    }

    private async Task AddDraggableMarker()
    {
        LeafletLatLong Point = new(latitude, longitude);
        lastIndex = await myMap.AddDraggableMarker(Point, title, description, "img/destination.png");
        message = $"Marker with index {lastIndex} added.";
        StateHasChanged();
    }

    private async Task RemoveFirstMarker()
    {
        message = $"Marker with index 0 removed.";
        await myMap.RemoveMarker(0);
    }

    private async Task RemoveAllMarkers()
    {
        message = $"All markers have been removed";
        await myMap.RemoveAllMarkers();
    }

    private async Task OnCreatedMap(Map map)
    {
        myMap = map;
        await myMap.AddMarker(originalPoint, "Origin", "This is a store");
    }
}

Note:
If you need your own icon for the marker, it is recommended to be 32x32 pixels in size.

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.Leaflet.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.10 88 11/15/2024
1.0.9 179 10/16/2024
1.0.8 232 8/18/2024
1.0.7 311 7/20/2024
1.0.6 294 5/22/2024
1.0.5 112 4/24/2024
1.0.4 291 4/20/2024
1.0.3 114 4/20/2024
1.0.2 104 4/20/2024
1.0.1 115 4/20/2024
1.0.0 109 4/20/2024