Purlin.Maps.Clustering 2.0.1

Prefix Reserved
dotnet add package Purlin.Maps.Clustering --version 2.0.1                
NuGet\Install-Package Purlin.Maps.Clustering -Version 2.0.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="Purlin.Maps.Clustering" Version="2.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Purlin.Maps.Clustering --version 2.0.1                
#r "nuget: Purlin.Maps.Clustering, 2.0.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.
// Install Purlin.Maps.Clustering as a Cake Addin
#addin nuget:?package=Purlin.Maps.Clustering&version=2.0.1

// Install Purlin.Maps.Clustering as a Cake Tool
#tool nuget:?package=Purlin.Maps.Clustering&version=2.0.1                

Purlin.Maps.Clustering

C# library for clustering map points for server side

Clustering Img

Original Lib
This is a fork of GoogleMaps.AspNetCore.Clustering repo. The project was no longer powered since Jul 20, 2018.

Installation

You can download the Purlin.Maps.Clustering package to install the latest version of Purlin.Maps.Clustering Lib.

Features

1․ Server-side clustering

2․ K-nearest neighbors - (Haversine formula)

Usage

First, configure Program.cs class:

using Purlin.GoogleMaps.Clustering.Data.Configuration;
using Purlin.GoogleMaps.Clustering.Data.Params;
using Purlin.GoogleMaps.Clustering.Sample.Lib;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}
app.UseHttpsRedirection();


app.UseGmc(builder.Configuration.GetSection("GoogleMapsNetClustering"));

You will also need to add the following section to your appsettings.json file.

"GoogleMapsNetClustering": {
    "DoShowGridLinesInGoogleMap": false,
    "OuterGridExtend": 0,
    "DoUpdateAllCentroidsToNearestContainingPoint": false,
    "DoMergeGridIfCentroidsAreCloseToEachOther": true,
    "CacheServices": true,
    "MergeWithin": 2.9,
    "MinClusterSize": 2,
    "MaxMarkersReturned": 500,
    "AlwaysClusteringEnabledWhenZoomLevelLess": 2,
    "ZoomlevelClusterStop": 15,
    "GridX": 6,
    "GridY": 5,
    "MarkerTypes": [1,2,3],
    "MaxPointsInCache": 100000000
  },

Next, this is an example of how to used cached clustering.

public static class SampleCluster
{
    public static async Task<IList<MapPoint>> Clustering(GetMarkersParams markersParams = null!)
    {
        try
        {
            using var r = new StreamReader("path to file");
            var data = await r.ReadToEndAsync();
            var list = JsonSerializer.Deserialize<List<Point>>(data);

            var marker = GetClusters(list, markersParams);
            return marker;
        }
        catch
        {
            return new List<MapPoint>();
        }
    }

    private static IList<MapPoint> GetClusters(IEnumerable<Point> data, GetMarkersParams markersParams = null)
    {
        const string clusterPointsCacheKey = "somecachekey";
        var points = GetClusterPointCollection(data, clusterPointsCacheKey);

        var mapService = new ClusterService(points);

        var option = markersParams ?? new GetMarkersParams()
        {

            ZoomLevel = 6,
            PointType = clusterPointsCacheKey,
        };

        var markers = mapService.GetClusterMarkers(option);

        return markers.Markers;
    }

    private static PointCollection GetClusterPointCollection(IEnumerable<Point> data, string clusterPointsCacheKey)
    {
        var points = new PointCollection();

        var dbPoints = data.Select(p => new MapPoint
        {
            X = p.Lng!.Value,
            Y = p.Lat!.Value
        }).ToList();
        var cacheDuration = TimeSpan.FromHours(6);
        points.Set(dbPoints, cacheDuration, clusterPointsCacheKey);

        return points;
    }
}

public class Point
{
    public float? Lat { get; set; }
    public float? Lng { get; set; }
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp2.1 is compatible.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 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
2.0.1 309 8/24/2023
1.0.4 160 8/23/2023
1.0.2 268 3/23/2023
1.0.1 213 3/23/2023
1.0.0 230 3/23/2023

Initial release of the package.