Compute a distance matrix (Go)

Compute an origins-to-destinations matrix via the Thinwrap Matrix facade in Go — one call shape for Mapbox, Google, HERE and OSRM.

1. Install

go get github.com/thinwrap/location-go

2. Send the call

import "github.com/thinwrap/location-go"

m := location.NewMatrix(location.MapboxConfig{
    AccessToken: os.Getenv("MAPBOX_ACCESS_TOKEN"),
})

// Providers bill per origin x destination pair — keep probe matrices small.
res, err := m.Matrix(ctx, location.MatrixOptions{
    Origins: []location.LatLng{
        {Lat: 52.5200, Lng: 13.4050},
    },
    Destinations: []location.LatLng{
        {Lat: 52.5163, Lng: 13.3777},
        {Lat: 52.5096, Lng: 13.3765},
    },
    TravelMode: location.Driving,
})
if err != nil {
    var ce *location.ConnectorError
    if errors.As(err, &ce) {
        log.Printf("%s: %s", ce.ProviderCode, ce.ProviderMessage)
    }
    return
}

for _, cell := range res.Cells {
    fmt.Println(cell.OriginIndex, cell.DestinationIndex,
        cell.DistanceMeters, cell.DurationSeconds)
}

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