# 計算路徑範例

``````https://routespreferred.googleapis.com/v1:computeRoutes
``````

## 要求主體範例

``````POST /v1:computeRoutes
Content-Type: application/json
X-Goog-Api-Key: YOUR_API_KEY

{
"origin":{
"location":{
"latLng":{
"latitude": 37.419734,
"longitude": -122.0827784
}
}
},
"destination":{
"location":{
"latLng":{
"latitude": 37.417670,
"longitude": -122.079595
}
}
},
"travelMode": "DRIVE",
"routingPreference": "TRAFFIC_AWARE",
"polylineQuality": "OVERVIEW",
"departureTime": "2019-10-15T15:01:23.045123456Z",
"computeAlternativeRoutes": false,
"routeModifiers": {
"avoidTolls": false,
"avoidHighways": false,
"avoidFerries": false
},
"languageCode": "en-US",
"units": "IMPERIAL"
}
``````

## 回應主體範例

``````{
"routes": [
{
"distanceMeters": 772,
"duration": "165s",
"polyline": {
"encodedPolyline": "ipkcFfichVnP@j@BLoFVwM{E?"
}
}
]
}
``````

## gRPC 要求範例

### 查看

```package main

import (
"context"
"fmt"
"log"
"time"

)

const (
credentialsFile = "service-account.json"
// Note that setting the field mask to * is OK for testing, but discouraged in
// production.
// For example, for ComputeRoutes, set the field mask to
// "routes.distanceMeters,routes.duration,routes.polyline.encodedPolyline"
// in order to get the route distances, durations, and encoded polylines.
)

func main() {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

// instantiate a client
c, err := routespreferred.NewRoutesPreferredClient(ctx,
option.WithCredentialsFile(credentialsFile))
defer c.Close()

if err != nil {
log.Fatal(err)
}

// create the origin using a latitude and longitude
origin := &routespb.Waypoint{
LocationType: &routespb.Waypoint_Location{
Location: &routespb.Location{
LatLng: &latlng.LatLng{
Latitude:  37.417670,
Longitude: -122.0827784,
},
},
},
}

// create the destination using a latitude and longitude
destination := &routespb.Waypoint{
LocationType: &routespb.Waypoint_Location{
Location: &routespb.Location{
LatLng: &latlng.LatLng{
Latitude:  37.417670,
Longitude: -122.079595,
},
},
},
}

// create the request with additional options
req := &routespb.ComputeRoutesRequest{
Origin:                   origin,
Destination:              destination,
TravelMode:               routespb.RouteTravelMode_DRIVE,
RoutingPreference:        routespb.RoutingPreference_TRAFFIC_AWARE,
ComputeAlternativeRoutes: true,
Units:                    routespb.Units_METRIC,
LanguageCode:             "en-us",
RouteModifiers: &routespb.RouteModifiers{
AvoidTolls:    false,
AvoidHighways: true,
AvoidFerries:  true,
},
PolylineQuality: routespb.PolylineQuality_OVERVIEW,
}

// execute rpc
resp, err := c.ComputeRoutes(ctx, req)

if err != nil {
// "rpc error: code = InvalidArgument desc = Request contains an invalid
log.Fatal(err)
}

fmt.Printf("Duration of route %d", resp.Routes[0].Duration.Seconds)
}
main.go```

## 過路費的計算方式範例

### 要求：

``````curl -X POST -d '{
"origin":{
"location":{
"lat_lng":{
"latitude":47.7020056,
"longitude":-122.3479236
}
}
},
"destination":{
"location":{
"lat_lng":{
"latitude":47.6192234,
"longitude": -122.1676792
}
}
},
"travel_mode":"DRIVE",
"route_modifiers":{
"vehicle_info":{
"emission_type": "GASOLINE"
},
"toll_passes": [
"US_MA_EZPASSMA",
"US_WA_GOOD_TO_GO"
]
}
}' \
-H 'Content-Type: application/json' \
-H 'X-Goog-Api-Key: <YOUR_API_KEY>' \
``````

### 回應：

``````{
"routes": [
{
"legs": [
{
"tollInfo": {
"estimatedPrice": [
{
"currencyCode": "USD",
"units": "3",
"nanos": 400000000
}
]
}
}
}
],
"distanceMeters": 22496,
"duration": "1400s",
"tollInfo": {
"estimatedPrice": [
{
"currencyCode": "USD",
"units": "3",
"nanos": 400000000
}
]
}
}
}
]
}
``````
[]
[]