Simplify tomtom API for getting routes
This commit is contained in:
parent
2a17c5c133
commit
61737d0288
3 changed files with 86 additions and 177 deletions
178
tomtom/route.go
178
tomtom/route.go
|
|
@ -3,120 +3,71 @@ package tomtom
|
|||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/google/go-querystring/query"
|
||||
)
|
||||
|
||||
// CalculateRouteRequest combines both path parameters and POST body
|
||||
type CalculateRouteRequest struct {
|
||||
Params CalculateRouteParams
|
||||
PostData *CalculateRoutePostData
|
||||
// Path parameters
|
||||
Locations Locations
|
||||
|
||||
// Query parameters
|
||||
MaxAlternatives *int
|
||||
AlternativeType string
|
||||
MinDeviationDistance *int
|
||||
MinDeviationTime *int
|
||||
InstructionsType string
|
||||
Language string
|
||||
ComputeBestOrder *bool
|
||||
RouteRepresentation string
|
||||
ComputeTravelTimeFor string
|
||||
VehicleHeading *int
|
||||
SectionType []string
|
||||
IncludeTollPaymentTypes string
|
||||
Callback string
|
||||
Report string
|
||||
DepartAt string
|
||||
ArriveAt string
|
||||
RouteType string
|
||||
Traffic *bool
|
||||
Avoid []string
|
||||
TravelMode string
|
||||
Hilliness string
|
||||
Windingness string
|
||||
VehicleMaxSpeed *int
|
||||
VehicleWeight *int
|
||||
VehicleAxleWeight *int
|
||||
VehicleNumberOfAxles *int
|
||||
VehicleLength *float64
|
||||
VehicleWidth *float64
|
||||
VehicleHeight *float64
|
||||
VehicleCommercial *bool
|
||||
VehicleLoadType []string
|
||||
VehicleAdrTunnelRestrictionCode string
|
||||
VehicleHasElectricTollCollectionTransponder string
|
||||
VehicleEngineType string
|
||||
ConstantSpeedConsumptionInLitersPerHundredkm string
|
||||
CurrentFuelInLiters *float64
|
||||
AuxiliaryPowerInLitersPerHour *float64
|
||||
FuelEnergyDensityInMJoulesPerLiter *float64
|
||||
AccelerationEfficiency *float64
|
||||
DecelerationEfficiency *float64
|
||||
UphillEfficiency *float64
|
||||
DownhillEfficiency *float64
|
||||
ConsumptionInkWhPerkmAltitudeGain *float64
|
||||
RecuperationInkWhPerkmAltitudeLoss *float64
|
||||
ConstantSpeedConsumptionInkWhPerHundredkm string
|
||||
CurrentChargeInkWh *float64
|
||||
MaxChargeInkWh *float64
|
||||
AuxiliaryPowerInkW *float64
|
||||
}
|
||||
|
||||
func (sgr CalculateRouteRequest) toQueryParams() (url.Values, error) {
|
||||
return query.Values(sgr)
|
||||
}
|
||||
|
||||
// BuildURL builds the URL for the Calculate Route request
|
||||
func (req *CalculateRouteRequest) BuildURL(apiKey string) (string, error) {
|
||||
baseURL := fmt.Sprintf("%s/routing/%d/calculateRoute/%s/%s",
|
||||
BaseURL,
|
||||
req.Params.VersionNumber,
|
||||
req.Params.Locations,
|
||||
req.Params.ContentType)
|
||||
|
||||
// Add query parameters
|
||||
query := url.Values{}
|
||||
query.Add("key", apiKey)
|
||||
|
||||
// Add all the query parameters if they're set
|
||||
if req.Params.MaxAlternatives != nil {
|
||||
query.Add("maxAlternatives", strconv.Itoa(*req.Params.MaxAlternatives))
|
||||
}
|
||||
|
||||
if req.Params.AlternativeType != "" {
|
||||
query.Add("alternativeType", req.Params.AlternativeType)
|
||||
}
|
||||
|
||||
if req.Params.MinDeviationDistance != nil {
|
||||
query.Add("minDeviationDistance", strconv.Itoa(*req.Params.MinDeviationDistance))
|
||||
}
|
||||
|
||||
if req.Params.MinDeviationTime != nil {
|
||||
query.Add("minDeviationTime", strconv.Itoa(*req.Params.MinDeviationTime))
|
||||
}
|
||||
|
||||
if req.Params.InstructionsType != "" {
|
||||
query.Add("instructionsType", req.Params.InstructionsType)
|
||||
}
|
||||
|
||||
if req.Params.Language != "" {
|
||||
query.Add("language", req.Params.Language)
|
||||
}
|
||||
|
||||
if req.Params.ComputeBestOrder != nil {
|
||||
query.Add("computeBestOrder", strconv.FormatBool(*req.Params.ComputeBestOrder))
|
||||
}
|
||||
|
||||
if req.Params.RouteRepresentation != "" {
|
||||
query.Add("routeRepresentation", req.Params.RouteRepresentation)
|
||||
}
|
||||
|
||||
if req.Params.ComputeTravelTimeFor != "" {
|
||||
query.Add("computeTravelTimeFor", req.Params.ComputeTravelTimeFor)
|
||||
}
|
||||
|
||||
if req.Params.VehicleHeading != nil {
|
||||
query.Add("vehicleHeading", strconv.Itoa(*req.Params.VehicleHeading))
|
||||
}
|
||||
|
||||
for _, sectionType := range req.Params.SectionType {
|
||||
query.Add("sectionType", sectionType)
|
||||
}
|
||||
|
||||
if req.Params.IncludeTollPaymentTypes != "" {
|
||||
query.Add("includeTollPaymentTypes", req.Params.IncludeTollPaymentTypes)
|
||||
}
|
||||
|
||||
if req.Params.Callback != "" {
|
||||
query.Add("callback", req.Params.Callback)
|
||||
}
|
||||
|
||||
if req.Params.Report != "" {
|
||||
query.Add("report", req.Params.Report)
|
||||
}
|
||||
|
||||
if req.Params.DepartAt != "" {
|
||||
query.Add("departAt", req.Params.DepartAt)
|
||||
}
|
||||
|
||||
if req.Params.ArriveAt != "" {
|
||||
query.Add("arriveAt", req.Params.ArriveAt)
|
||||
}
|
||||
|
||||
if req.Params.RouteType != "" {
|
||||
query.Add("routeType", req.Params.RouteType)
|
||||
}
|
||||
|
||||
if req.Params.Traffic != nil {
|
||||
query.Add("traffic", strconv.FormatBool(*req.Params.Traffic))
|
||||
}
|
||||
|
||||
for _, avoid := range req.Params.Avoid {
|
||||
query.Add("avoid", avoid)
|
||||
}
|
||||
|
||||
if req.Params.TravelMode != "" {
|
||||
query.Add("travelMode", req.Params.TravelMode)
|
||||
}
|
||||
|
||||
// Add other parameters similarly...
|
||||
// Too many to list all here, but the pattern is the same
|
||||
|
||||
return baseURL + "?" + query.Encode(), nil
|
||||
}
|
||||
|
||||
// CalculateRoute sends a route calculation request to TomTom API
|
||||
func (c *TomTom) CalculateRoute(req *CalculateRouteRequest) (*CalculateRouteResponse, error) {
|
||||
/*url, err := req.BuildURL(c.APIKey)
|
||||
|
|
@ -135,7 +86,7 @@ func (c *TomTom) CalculateRoute(req *CalculateRouteRequest) (*CalculateRouteResp
|
|||
resp, err := c.client.R().
|
||||
//SetQueryParamsFromValues(query).
|
||||
SetResult(&result).
|
||||
SetPathParam("locations", req.Params.Locations).
|
||||
SetPathParam("locations", locationString(req.Locations)).
|
||||
SetPathParam("urlBase", c.urlBase).
|
||||
SetQueryParam("key", c.APIKey).
|
||||
Get("https://{urlBase}/routing/1/calculateRoute/{locations}/json")
|
||||
|
|
@ -148,3 +99,24 @@ func (c *TomTom) CalculateRoute(req *CalculateRouteRequest) (*CalculateRouteResp
|
|||
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
func P(lat, lng float64) Point {
|
||||
return Point{
|
||||
Latitude: lat,
|
||||
Longitude: lng,
|
||||
}
|
||||
}
|
||||
|
||||
type Locations = []Point
|
||||
|
||||
func locationString(locations Locations) string {
|
||||
var sb strings.Builder
|
||||
for i, p := range locations {
|
||||
if i == 0 {
|
||||
sb.WriteString(fmt.Sprintf("%f,%f", p.Latitude, p.Longitude))
|
||||
} else {
|
||||
sb.WriteString(fmt.Sprintf(":%f,%f", p.Latitude, p.Longitude))
|
||||
}
|
||||
}
|
||||
return sb.String()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue