Compute a distance matrix (Elixir)

Compute an origins-to-destinations matrix via :thinwrap_location.matrix/2 in Elixir — one call shape for Mapbox, Google, HERE, ESRI, TomTom and OSRM.

1. Install

{:thinwrap_location, "~> 1.3"}

2. Send the call

config = %{provider: :mapbox, access_token: System.fetch_env!("MAPBOX_ACCESS_TOKEN")}

# Providers bill per origin x destination pair — keep probe matrices small.
case :thinwrap_location.matrix(config, %{
       origins: [%{lat: 52.5200, lng: 13.4050}],
       destinations: [%{lat: 52.5163, lng: 13.3777}, %{lat: 52.5096, lng: 13.3765}],
       travel_mode: :driving
     }) do
  {:ok, %{cells: cells}} ->
    for cell <- cells do
      IO.puts(
        "#{cell.origin_index} #{cell.destination_index} " <>
          "#{cell.distance_meters} #{cell.duration_seconds}"
      )
    end

  {:error, %{code: code, message: message}} ->
    IO.puts("#{code}: #{message}")
end

Same code works for all supported providers — just change the provider ID.