計算路徑矩陣範例

您可以使用 ComputeRouteMatrix 方法傳送 HTTP 要求和回應,也可以使用任何支援 gRPC 的語言,包括 Java 和 Go。

HTTP 範例

以下範例顯示 ComputeRouteMatrix HTTP 要求和回應。

要求:

curl -X POST -d '{
"origins": [
{
"waypoint": { "location": { "latLng": {
"latitude": 37.420761,
"longitude": -122.081356,
}}},
"routeModifiers": { "avoidFerries": true}
},
{
"waypoint": { "location": { "latLng": {
"latitude": 37.403184,
"longitude": -122.097371,
}}},
"routeModifiers": { "avoidFerries": true}
}
],
"destinations": [
{
"waypoint": { "location": { "latLng": {
"latitude": 37.420999,
"longitude": -122.086894,
}}}
},
{
"waypoint": { "location": { "latLng": {
"latitude": 37.383047,
"longitude": -122.044651,
}}}
}
],
"travelMode": "DRIVE",
"routingPreference": "TRAFFIC_AWARE"
}' -H 'Content-Type: application/json' -H 'X-Goog-Api-Key: <YOUR_API_KEY>' -H 'X-Goog-FieldMask: originIndex,destinationIndex,duration,distanceMeters,status' 'https://routespreferred.googleapis.com/v1alpha:computeRouteMatrix'

回應:

[{
  "status": {},
  "distanceMeters": 827,
  "duration": "139s"
}
,
{
  "originIndex": 1,
  "destinationIndex": 1,
  "status": {},
  "distanceMeters": 5597,
  "duration": "383s"
}
,
{
  "originIndex": 1,
  "status": {},
  "distanceMeters": 2926,
  "duration": "316s"
}
,
{
  "destinationIndex": 1,
  "status": {},
  "distanceMeters": 8602,
  "duration": "613s"
}
]

以下範例顯示用於計算預估過路費價格的 ComputeRouteMatrix HTTP 要求和回應。

要求:

curl -X POST -d '{
"origins": [
{
"waypoint": { "location": { "latLng": {
        "latitude":47.7020056,
        "longitude":-122.3479236,
}}},
"routeModifiers": {
"vehicleInfo":{
      "emissionType": "GASOLINE"
    },
    "tollPasses": [
      "US_MA_EZPASSMA",
      "US_WA_GOOD_TO_GO"
    ]
}
}],
"destinations": [
{
"waypoint": { "location": { "latLng": {
        "latitude":47.6192234,
        "longitude": -122.1676792
}}}
}],
"travelMode": "DRIVE",
"routingPreference": "TRAFFIC_AWARE"
}' -H 'Content-Type: application/json' -H 'X-Goog-Api-Key: <YOUR_API_KEY>' -H 'X-Goog-FieldMask: originIndex,destinationIndex,travelAdvisory,duration,distanceMeters,status' 'https://routespreferred.googleapis.com/v1alpha:computeRouteMatrix'

回應:

[{
  "status": {},
  "distanceMeters": 22498,
  "duration": "1251s",
  "travelAdvisory": {
    "tollInfo": {
      "estimatedPrice": [
        {
          "currencyCode": "USD",
          "units": "2",
          "nanos": 700000000
        }
      ]
    }
  }
}
]

Java 和 Go 範例

以下範例說明如何使用 Java 或 Go 呼叫 ComputeRouteMatrix 方法。如需建構指示,請參閱對應的 JavaGo Github 存放區。

Java

package com.example;

import com.google.maps.routes.v1.*;
import com.google.type.LatLng;
import io.grpc.CallOptions;
import io.grpc.Channel;
import io.grpc.ClientCall;
import io.grpc.ClientInterceptor;
import io.grpc.ClientInterceptors;
import io.grpc.ForwardingClientCall;
import io.grpc.Metadata;
import io.grpc.MethodDescriptor;
import io.grpc.StatusRuntimeException;
import io.grpc.netty.NettyChannelBuilder;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

public class RoutesPreferredClient {
    // For more detail on inserting API keys, see:
    // https://cloud.google.com/endpoints/docs/grpc/restricting-api-access-with-api-keys#java
    // For more detail on system parameters (such as FieldMask), see:
    // https://cloud.google.com/apis/docs/system-parameters
    private static final class RoutesPreferredInterceptor implements ClientInterceptor {
        private final String apiKey;
        private static final Logger logger = Logger.getLogger(RoutesPreferredInterceptor.class.getName());
        private static Metadata.Key<String> API_KEY_HEADER = Metadata.Key.of("x-goog-api-key",
                Metadata.ASCII_STRING_MARSHALLER);
        private static Metadata.Key<String> FIELD_MASK_HEADER = Metadata.Key.of("x-goog-fieldmask",
                Metadata.ASCII_STRING_MARSHALLER);

        public RoutesPreferredInterceptor(String apiKey) {
            this.apiKey = apiKey;
        }

        @Override
        public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method,
                CallOptions callOptions, Channel next) {
            logger.info("Intercepted " + method.getFullMethodName());
            ClientCall<ReqT, RespT> call = next.newCall(method, callOptions);
            call = new ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT>(call) {
                @Override
                public void start(Listener<RespT> responseListener, Metadata headers) {
                    headers.put(API_KEY_HEADER, apiKey);
                    // 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.
                    headers.put(FIELD_MASK_HEADER, "*");
                    super.start(responseListener, headers);
                }
            };
            return call;
        }
    }

    private static final Logger logger = Logger.getLogger(RoutesPreferredClient.class.getName());
    private final RoutesPreferredGrpc.RoutesPreferredBlockingStub blockingStub;

    public RoutesPreferredClient(Channel channel) {
        blockingStub = RoutesPreferredGrpc.newBlockingStub(channel);
    }

    public static Waypoint createWaypointForLatLng(double lat, double lng) {
        return Waypoint.newBuilder()
                .setLocation(Location.newBuilder().setLatLng(LatLng.newBuilder().setLatitude(lat).setLongitude(lng)))
                .build();
    }

    public void computeRoutes() {
        ComputeRoutesRequest request = ComputeRoutesRequest.newBuilder()
                .setOrigin(createWaypointForLatLng(37.420761, -122.081356))
                .setDestination(createWaypointForLatLng(37.420999, -122.086894)).setTravelMode(RouteTravelMode.DRIVE)
                .setRoutingPreference(RoutingPreference.TRAFFIC_AWARE).setComputeAlternativeRoutes(true)
                .setUnits(Units.METRIC).setLanguageCode("en-us")
                .setRouteModifiers(
                        RouteModifiers.newBuilder().setAvoidTolls(false).setAvoidHighways(true).setAvoidFerries(true))
                .setPolylineQuality(PolylineQuality.OVERVIEW).build();
        ComputeRoutesResponse response;
        try {
            logger.info("About to send request: " + request.toString());
            response = blockingStub.withDeadlineAfter(2000, TimeUnit.MILLISECONDS).computeRoutes(request);
        } catch (StatusRuntimeException e) {
            logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus());
            return;
        }
        logger.info("Response: " + response.toString());
    }

    public void computeRouteMatrix() {
        ComputeRouteMatrixRequest request = ComputeRouteMatrixRequest.newBuilder()
                .addOrigins(RouteMatrixOrigin.newBuilder().setWaypoint(createWaypointForLatLng(37.420761, -122.081356))
                        .setRouteModifiers(RouteModifiers.newBuilder().setAvoidTolls(false).setAvoidHighways(true)
                                .setAvoidFerries(true)))
                .addOrigins(RouteMatrixOrigin.newBuilder().setWaypoint(createWaypointForLatLng(37.403184, -122.097371)))
                .addDestinations(RouteMatrixDestination.newBuilder()
                        .setWaypoint(createWaypointForLatLng(37.420999, -122.086894)))
                .addDestinations(RouteMatrixDestination.newBuilder()
                        .setWaypoint(createWaypointForLatLng(37.383047, -122.044651)))
                .setTravelMode(RouteTravelMode.DRIVE).setRoutingPreference(RoutingPreference.TRAFFIC_AWARE).build();
        Iterator<RouteMatrixElement> elements;
        try {
            logger.info("About to send request: " + request.toString());
            elements = blockingStub.withDeadlineAfter(2000, TimeUnit.MILLISECONDS).computeRouteMatrix(request);
        } catch (StatusRuntimeException e) {
            logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus());
            return;
        }

        while (elements.hasNext()) {
            logger.info("Element response: " + elements.next().toString());
        }
    }

    public static void main(String[] args) throws Exception {
        String apiKey = System.getenv("GOOGLE_MAPS_API_KEY");

        // The standard TLS port is 443
        Channel channel = NettyChannelBuilder.forAddress("routespreferred.googleapis.com", 443).build();
        channel = ClientInterceptors.intercept(channel, new RoutesPreferredInterceptor(apiKey));

        RoutesPreferredClient client = new RoutesPreferredClient(channel);
        client.computeRoutes();
        client.computeRouteMatrix();
    }
}
  

Go

package main

import (
	"context"
	"crypto/tls"
	"io"
	"log"
	"os"
	"time"

	"github.com/golang/protobuf/proto"
	v1 "google.golang.org/genproto/googleapis/maps/routes/v1"
	"google.golang.org/genproto/googleapis/type/latlng"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials"
	"google.golang.org/grpc/metadata"
)

const (
	serverAddr = "routespreferred.googleapis.com:443"
	// 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.
	fieldMask = "*"
)

func createWaypoint(lat float64, lng float64) *v1.Waypoint {
	return &v1.Waypoint{LocationType: &v1.Waypoint_Location{
		Location: &v1.Location{
			LatLng: &latlng.LatLng{Latitude: lat, Longitude: lng},
		},
	}}
}

func callComputeRoutes(client v1.RoutesPreferredClient, ctx *context.Context) {
	request := v1.ComputeRoutesRequest{
		Origin:                   createWaypoint(37.420761, -122.081356),
		Destination:              createWaypoint(37.420999, -122.086894),
		TravelMode:               v1.RouteTravelMode_DRIVE,
		RoutingPreference:        v1.RoutingPreference_TRAFFIC_AWARE,
		ComputeAlternativeRoutes: true,
		Units:                    v1.Units_METRIC,
		LanguageCode:             "en-us",
		RouteModifiers: &v1.RouteModifiers{
			AvoidTolls:    false,
			AvoidHighways: true,
			AvoidFerries:  true,
		},
		PolylineQuality: v1.PolylineQuality_OVERVIEW,
	}
	marshaler := proto.TextMarshaler{}
	log.Printf("Sending request: \n%s", marshaler.Text(&request))
	result, err := client.ComputeRoutes(*ctx, &request)

	if err != nil {
		log.Fatalf("Failed to call ComputeRoutes: %v", err)
	}
	log.Printf("Result: %s", marshaler.Text(result))
}

func callComputeRouteMatrix(client v1.RoutesPreferredClient, ctx *context.Context) {
	request := v1.ComputeRouteMatrixRequest{
		Origins: []*v1.RouteMatrixOrigin{
			{Waypoint: createWaypoint(37.420761, -122.081356), RouteModifiers: &v1.RouteModifiers{
				AvoidTolls:    false,
				AvoidHighways: true,
				AvoidFerries:  true,
			}},
			{Waypoint: createWaypoint(37.403184, -122.097371)},
		},
		Destinations: []*v1.RouteMatrixDestination{
			{Waypoint: createWaypoint(37.420999, -122.086894)},
			{Waypoint: createWaypoint(37.383047, -122.044651)},
		},
		TravelMode:        v1.RouteTravelMode_DRIVE,
		RoutingPreference: v1.RoutingPreference_TRAFFIC_AWARE,
	}
	marshaler := proto.TextMarshaler{}
	log.Printf("Sending request: \n%s", marshaler.Text(&request))
	stream, err := client.ComputeRouteMatrix(*ctx, &request)

	if err != nil {
		log.Fatalf("Failed to call ComputeRouteMatrix: %v", err)
	}
	for {
		element, err := stream.Recv()
		if err == io.EOF {
			break
		}
		if err != nil {
			log.Fatalf("Received error in ComputeRouteMatrix stream: %v", err)
		}
		log.Printf("Element: %s\n", marshaler.Text(element))
	}
}

func main() {
	config := tls.Config{}
	conn, err := grpc.Dial(serverAddr,
		grpc.WithTransportCredentials(credentials.NewTLS(&config)))
	if err != nil {
		log.Fatalf("Failed to connect: %v", err)
	}
	defer conn.Close()
	client := v1.NewRoutesPreferredClient(conn)
	ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
	ctx = metadata.AppendToOutgoingContext(ctx, "X-Goog-Api-Key", os.Getenv("GOOGLE_MAPS_API_KEY"))
	ctx = metadata.AppendToOutgoingContext(ctx, "X-Goog-Fieldmask", fieldMask)
	defer cancel()

	callComputeRoutes(client, &ctx)
	callComputeRouteMatrix(client, &ctx)
}
  

計算通行費的範例

以下範例使用 computeRouteMatrix 方法,傳回路線上的通行費資訊,以及使用通行卡時的預估價格。

這項功能會透過要求中指定的 routes.travelAdvisory.tollInfo 欄位遮罩啟用。通行費通行證會在 route_modifiers 欄位中指定。系統會根據指定通行證使用的價格,傳回通行費價格。如果指定多個通行證,系統會傳回最便宜的價格。

要求:

curl -X POST -d '{
"origins": [
{
"waypoint": { "location": { "latLng": {
        "latitude":47.7020056,
        "longitude":-122.3479236,
}}},
"routeModifiers": {
"vehicleInfo":{
      "emissionType": "GASOLINE"
    },
    "tollPasses": [
      "US_MA_EZPASSMA",
      "US_WA_GOOD_TO_GO"
    ]
}
}],
"destinations": [
{
"waypoint": { "location": { "latLng": {
        "latitude":47.6192234,
        "longitude": -122.1676792
}}}
}],
"travelMode": "DRIVE",
"routingPreference": "TRAFFIC_AWARE"
}' \
-H 'Content-Type: application/json'  \
-H 'X-Goog-Api-Key: <YOUR_API_KEY>'  \
-H 'X-Goog-FieldMask: originIndex,destinationIndex,travelAdvisory,duration,distanceMeters,status' 'https://routespreferred.googleapis.com/v1alpha:computeRouteMatrix'

回應:

[{
  "status": {},
  "distanceMeters": 22495,
  "duration": "1446s",
  "travelAdvisory": {
    "tollInfo": {
      "estimatedPrice": [
        {
          "currencyCode": "USD",
          "units": "4",
          "nanos": 300000000
        }
      ]
    }
  }
}]