Send OSRM map match request

CSharp · Console

/Program.cs

using System.Net.Http.Json;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
using System;
// install the Microsoft.Extensions.Http Nuget package
using Microsoft.Extensions.DependencyInjection;

namespace OsrmMapMatch
{
    // Visual Studio menu: Edit - Paste Special - Paste JSON as classes
    public class OsrmResponse
    {
        public string code { get; set; }
        public Matching[] matchings { get; set; }
    }

    public class Matching
    {
        public Leg[] legs { get; set; }
    }
    public class Leg
    {
        public Annotation annotation { get; set; }
    }

    public class Annotation
    {
        public long[] nodes { get; set; }
    }


    internal class Program
    {
        static async Task Main(string[] args)
        {
            // part 1: setup http factory

            const string HttpClientMapMatch = "HttpClientMapMatch";
            const string OsrmUri = "10.757938,52.437444;10.764379,52.437314;10.770562,52.439067;10.773268,52.436633?overview=simplified&radiuses=50;50;50;50&generate_hints=false&skip_waypoints=true&gaps=ignore&annotations=nodes&geometries=geojson";

            ServiceProvider serviceProvider = new ServiceCollection()
                .AddHttpClient(HttpClientMapMatch, httpClient =>
                {
                    httpClient.BaseAddress = new Uri("https://router.project-osrm.org/match/v1/driving/");
                }).Services.BuildServiceProvider();

            IHttpClientFactory? httpClientFactory = serviceProvider.GetService<IHttpClientFactory>();
            HttpClient? httpClient = httpClientFactory?.CreateClient(HttpClientMapMatch);
            if (httpClient == null)
            {
                Console.WriteLine("Error httpClient is null");
                return;
            }

            // part 2: use GetAsync which allows checking HTTP response code

            HttpResponseMessage response = await httpClient.GetAsync(OsrmUri);
            if (response.IsSuccessStatusCode)
            {
                string stringData = await response.Content.ReadAsStringAsync();
                JsonSerializerOptions options = new()
                {
                    PropertyNameCaseInsensitive = true,

                };
                OsrmResponse? osrmResponse = JsonSerializer.Deserialize<OsrmResponse>(stringData, options);
                PrintNodes(osrmResponse);
            }

            // part 3: use GetFromJsonAsync as a shorter alternative

            OsrmResponse? osrmResponse2 = await httpClient.GetFromJsonAsync<OsrmResponse>(OsrmUri);
            PrintNodes(osrmResponse2);
        }

        private static void PrintNodes(OsrmResponse? osrmResponse)
        {
            Console.WriteLine($"osrmResponse: {JsonSerializer.Serialize(osrmResponse)}\n");
            if (osrmResponse?.code == "Ok")
            {
                foreach (Matching matching in osrmResponse.matchings)
                {
                    foreach (Leg leg in matching.legs)
                    {
                        Console.WriteLine($"nodes: {JsonSerializer.Serialize(leg.annotation.nodes)}\n");
                    }
                }
            }
        }
    }
}
;