diff --git a/routing.go b/routing.go index c1e2c1c..7804dcd 100644 --- a/routing.go +++ b/routing.go @@ -2,6 +2,7 @@ package routing import ( "fmt" + "time" "github.com/paulmach/orb" ) @@ -20,8 +21,15 @@ func NewRoutingService(service_type string, baseUrl string) (RoutingService, err type Route struct { Summary RouteSummary + Legs []RouteLeg } type RouteSummary struct { Polyline string } + +type RouteLeg struct { + Distance float64 + Duration time.Duration + Polyline string +} diff --git a/valhalla.go b/valhalla.go index 95fc33b..2eb9cdd 100644 --- a/valhalla.go +++ b/valhalla.go @@ -5,6 +5,7 @@ import ( "errors" "io/ioutil" "net/http" + "time" "git.coopgo.io/coopgo-platform/routing-service/encoding/polylines" "git.coopgo.io/coopgo-platform/routing-service/proto/valhalla" @@ -55,16 +56,27 @@ func (v *ValhallaRouting) Route(locations []orb.Point) (route *Route, err error) decodedLinestring := orb.LineString{} + legs := []RouteLeg{} + for _, leg := range resp.Directions.Routes[0].Legs { shape := leg.Shape decodedShape := polylines.Decode(&shape, 6) decodedLinestring = append(decodedLinestring, decodedShape...) + + routeLeg := RouteLeg{ + Distance: float64(leg.Summary.Length), + Duration: time.Duration(leg.Summary.Time) * time.Second, + Polyline: polylines.Encode(decodedShape), + } + + legs = append(legs, routeLeg) } return &Route{ Summary: RouteSummary{ Polyline: polylines.Encode(decodedLinestring), }, + Legs: legs, }, nil }