Class v2.RoutesClient (0.3.1)

The Routes API. v2

Package

@googlemaps/routing

Constructors

(constructor)(opts, gaxInstance)

constructor(opts?: ClientOptions, gaxInstance?: typeof gax | typeof gax.fallback);

Construct an instance of RoutesClient.

Parameters
NameDescription
opts ClientOptions
gaxInstance typeof gax | typeof gax.fallback

: loaded instance of google-gax. Useful if you need to avoid loading the default gRPC version and want to use the fallback HTTP implementation. Load only fallback version and pass it to the constructor: ``` const gax = require('google-gax/build/src/fallback'); // avoids loading google-gax with gRPC const client = new RoutesClient({fallback: 'rest'}, gax); ```

Properties

apiEndpoint

static get apiEndpoint(): string;

The DNS address for this API service - same as servicePath(), exists for compatibility reasons.

auth

auth: gax.GoogleAuth;

descriptors

descriptors: Descriptors;

innerApiCalls

innerApiCalls: {
        [name: string]: Function;
    };

port

static get port(): number;

The port for this API service.

routesStub

routesStub?: Promise<{
        [name: string]: Function;
    }>;

scopes

static get scopes(): never[];

The scopes needed to make gRPC calls for every method defined in this service.

servicePath

static get servicePath(): string;

The DNS address for this API service.

warn

warn: (code: string, message: string, warnType?: string) => void;

Methods

close()

close(): Promise<void>;

Terminate the gRPC channel and close the client.

The client will no longer be usable and all future behavior is undefined.

Returns
TypeDescription
Promise<void>

{Promise} A promise that resolves when the client is closed.

computeRouteMatrix(request, options)

computeRouteMatrix(request?: protos.google.maps.routing.v2.IComputeRouteMatrixRequest, options?: CallOptions): gax.CancellableStream;

Takes in a list of origins and destinations and returns a stream containing route information for each combination of origin and destination.

**NOTE:** This method requires that you specify a response field mask in the input. You can provide the response field mask by using the URL parameter $fields or fields, or by using the HTTP/gRPC header X-Goog-FieldMask (see the [available URL parameters and headers](https://cloud.google.com/apis/docs/system-parameters). The value is a comma separated list of field paths. See this detailed documentation about [how to construct the field paths](https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/field_mask.proto).

For example, in this method:

* Field mask of all available fields (for manual inspection): X-Goog-FieldMask: * * Field mask of route durations, distances, element status, condition, and element indices (an example production setup): X-Goog-FieldMask: originIndex,destinationIndex,status,condition,distanceMeters,duration

It is critical that you include status in your field mask as otherwise all messages will appear to be OK. Google discourages the use of the wildcard (*) response field mask, because:

* Selecting only the fields that you need helps our server save computation cycles, allowing us to return the result to you with a lower latency. * Selecting only the fields that you need in your production job ensures stable latency performance. We might add more response fields in the future, and those new fields might require extra computation time. If you select all fields, or if you select all fields at the top level, then you might experience performance degradation because any new field we add will be automatically included in the response. * Selecting only the fields that you need results in a smaller response size, and thus higher network throughput.

Parameters
NameDescription
request protos.google.maps.routing.v2.IComputeRouteMatrixRequest

The request object that will be sent.

options CallOptions

Call options. See CallOptions for more details.

Returns
TypeDescription
gax.CancellableStream

{Stream} An object stream which emits on 'data' event. Please see the [documentation](https://github.com/googleapis/gax-nodejs/blob/master/client-libraries.md#server-streaming) for more details and examples.

Example

  /**
   * This snippet has been automatically generated and should be regarded as a code template only.
   * It will require modifications to work.
   * It may require correct/in-range values for request initialization.
   * TODO(developer): Uncomment these variables before running the sample.
   */
  /**
   *  Required. Array of origins, which determines the rows of the response
   *  matrix. Several size restrictions apply to the cardinality of origins and
   *  destinations:
   *  * The number of elements (origins × destinations) must be no greater than
   *  625 in any case.
   *  * The number of elements (origins × destinations) must be no greater than
   *  100 if routing_preference is set to `TRAFFIC_AWARE_OPTIMAL`.
   *  * The number of waypoints (origins + destinations) specified as `place_id`
   *  must be no greater than 50.
   */
  // const origins = 1234
  /**
   *  Required. Array of destinations, which determines the columns of the
   *  response matrix.
   */
  // const destinations = 1234
  /**
   *  Optional. Specifies the mode of transportation.
   */
  // const travelMode = {}
  /**
   *  Optional. Specifies how to compute the route. The server attempts to use
   *  the selected routing preference to compute the route. If the routing
   *  preference results in an error or an extra long latency, an error is
   *  returned. You can specify this option only when the `travel_mode` is
   *  `DRIVE` or `TWO_WHEELER`, otherwise the request fails.
   */
  // const routingPreference = {}
  /**
   *  Optional. The departure time. If you don't set this value, this defaults to
   *  the time that you made the request. If you set this value to a time that
   *  has already occurred, the request fails.
   */
  // const departureTime = {}
  /**
   *  Optional. A list of extra computations which may be used to complete the
   *  request. Note: These extra computations may return extra fields on the
   *  response. These extra fields must also be specified in the field mask to be
   *  returned in the response.
   */
  // const extraComputations = 1234

  // Imports the Routing library
  const {RoutesClient} = require('@googlemaps/routing').v2;

  // Instantiates a client
  const routingClient = new RoutesClient();

  async function callComputeRouteMatrix() {
    // Construct request
    const request = {
      origins,
      destinations,
    };

    // Run request
    const stream = await routingClient.computeRouteMatrix(request);
    stream.on('data', (response) => { console.log(response) });
    stream.on('error', (err) => { throw(err) });
    stream.on('end', () => { /* API call completed */ });
  }

  callComputeRouteMatrix();

computeRoutes(request, options)

computeRoutes(request?: protos.google.maps.routing.v2.IComputeRoutesRequest, options?: CallOptions): Promise<[
        protos.google.maps.routing.v2.IComputeRoutesResponse,
        protos.google.maps.routing.v2.IComputeRoutesRequest | undefined,
        {} | undefined
    ]>;

Returns the primary route along with optional alternate routes, given a set of terminal and intermediate waypoints.

**NOTE:** This method requires that you specify a response field mask in the input. You can provide the response field mask by using URL parameter $fields or fields, or by using an HTTP/gRPC header X-Goog-FieldMask (see the [available URL parameters and headers](https://cloud.google.com/apis/docs/system-parameters). The value is a comma separated list of field paths. See detailed documentation about [how to construct the field paths](https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/field_mask.proto).

For example, in this method:

* Field mask of all available fields (for manual inspection): X-Goog-FieldMask: * * Field mask of Route-level duration, distance, and polyline (an example production setup): X-Goog-FieldMask: routes.duration,routes.distanceMeters,routes.polyline.encodedPolyline

Google discourage the use of the wildcard (*) response field mask, or specifying the field mask at the top level (routes), because:

* Selecting only the fields that you need helps our server save computation cycles, allowing us to return the result to you with a lower latency. * Selecting only the fields that you need in your production job ensures stable latency performance. We might add more response fields in the future, and those new fields might require extra computation time. If you select all fields, or if you select all fields at the top level, then you might experience performance degradation because any new field we add will be automatically included in the response. * Selecting only the fields that you need results in a smaller response size, and thus higher network throughput.

Parameters
NameDescription
request protos.google.maps.routing.v2.IComputeRoutesRequest

The request object that will be sent.

options CallOptions

Call options. See CallOptions for more details.

Returns
TypeDescription
Promise<[ protos.google.maps.routing.v2.IComputeRoutesResponse, protos.google.maps.routing.v2.IComputeRoutesRequest | undefined, {} | undefined ]>

{Promise} - The promise which resolves to an array. The first element of the array is an object representing . Please see the [documentation](https://github.com/googleapis/gax-nodejs/blob/master/client-libraries.md#regular-methods) for more details and examples.

Example

  /**
   * This snippet has been automatically generated and should be regarded as a code template only.
   * It will require modifications to work.
   * It may require correct/in-range values for request initialization.
   * TODO(developer): Uncomment these variables before running the sample.
   */
  /**
   *  Required. Origin waypoint.
   */
  // const origin = {}
  /**
   *  Required. Destination waypoint.
   */
  // const destination = {}
  /**
   *  Optional. A set of waypoints along the route (excluding terminal points),
   *  for either stopping at or passing by. Up to 25 intermediate waypoints are
   *  supported.
   */
  // const intermediates = 1234
  /**
   *  Optional. Specifies the mode of transportation.
   */
  // const travelMode = {}
  /**
   *  Optional. Specifies how to compute the route. The server
   *  attempts to use the selected routing preference to compute the route. If
   *   the routing preference results in an error or an extra long latency, then
   *  an error is returned. You can specify this option only when the
   *  `travel_mode` is `DRIVE` or `TWO_WHEELER`, otherwise the request fails.
   */
  // const routingPreference = {}
  /**
   *  Optional. Specifies your preference for the quality of the polyline.
   */
  // const polylineQuality = {}
  /**
   *  Optional. Specifies the preferred encoding for the polyline.
   */
  // const polylineEncoding = {}
  /**
   *  Optional. The departure time. If you don't set this value, then this value
   *  defaults to the time that you made the request. If you set this value to a
   *  time that has already occurred, then the request fails.
   */
  // const departureTime = {}
  /**
   *  Optional. Specifies whether to calculate alternate routes in addition to
   *  the route.
   */
  // const computeAlternativeRoutes = true
  /**
   *  Optional. A set of conditions to satisfy that affect the way routes are
   *  calculated.
   */
  // const routeModifiers = {}
  /**
   *  Optional. The BCP-47 language code, such as "en-US" or "sr-Latn". For more
   *  information, see
   *  http://www.unicode.org/reports/tr35/#Unicode_locale_identifier. See
   *  Language Support (https://developers.google.com/maps/faq#languagesupport)
   *  for the list of supported languages. When you don't provide this value, the
   *  display language is inferred from the location of the route request.
   */
  // const languageCode = 'abc123'
  /**
   *  Optional. Specifies the units of measure for the display fields. This
   *  includes the `instruction` field in `NavigationInstruction`. The units of
   *  measure used for the route, leg, step distance, and duration are not
   *  affected by this value. If you don't provide this value, then the display
   *  units are inferred from the location of the request.
   */
  // const units = {}
  /**
   *  Optional. Specifies what reference routes to calculate as part of the
   *  request in addition to the default route. A reference route is a route with
   *  a different route calculation objective than the default route. For example
   *  an FUEL_EFFICIENT reference route calculation takes into account various
   *  parameters that would generate an optimal fuel efficient route.
   */
  // const requestedReferenceRoutes = 1234
  /**
   *  Optional. A list of extra computations which may be used to complete the
   *  request. Note: These extra computations may return extra fields on the
   *  response. These extra fields must also be specified in the field mask to be
   *  returned in the response.
   */
  // const extraComputations = 1234

  // Imports the Routing library
  const {RoutesClient} = require('@googlemaps/routing').v2;

  // Instantiates a client
  const routingClient = new RoutesClient();

  async function callComputeRoutes() {
    // Construct request
    const request = {
      origin,
      destination,
    };

    // Run request
    const response = await routingClient.computeRoutes(request);
    console.log(response);
  }

  callComputeRoutes();

computeRoutes(request, options, callback)

computeRoutes(request: protos.google.maps.routing.v2.IComputeRoutesRequest, options: CallOptions, callback: Callback<protos.google.maps.routing.v2.IComputeRoutesResponse, protos.google.maps.routing.v2.IComputeRoutesRequest | null | undefined, {} | null | undefined>): void;
Parameters
NameDescription
request protos.google.maps.routing.v2.IComputeRoutesRequest
options CallOptions
callback Callback<protos.google.maps.routing.v2.IComputeRoutesResponse, protos.google.maps.routing.v2.IComputeRoutesRequest | null | undefined, {} | null | undefined>
Returns
TypeDescription
void

computeRoutes(request, callback)

computeRoutes(request: protos.google.maps.routing.v2.IComputeRoutesRequest, callback: Callback<protos.google.maps.routing.v2.IComputeRoutesResponse, protos.google.maps.routing.v2.IComputeRoutesRequest | null | undefined, {} | null | undefined>): void;
Parameters
NameDescription
request protos.google.maps.routing.v2.IComputeRoutesRequest
callback Callback<protos.google.maps.routing.v2.IComputeRoutesResponse, protos.google.maps.routing.v2.IComputeRoutesRequest | null | undefined, {} | null | undefined>
Returns
TypeDescription
void

getProjectId()

getProjectId(): Promise<string>;
Returns
TypeDescription
Promise<string>

getProjectId(callback)

getProjectId(callback: Callback<string, undefined, undefined>): void;
Parameter
NameDescription
callback Callback<string, undefined, undefined>
Returns
TypeDescription
void

initialize()

initialize(): Promise<{
        [name: string]: Function;
    }>;

Initialize the client. Performs asynchronous operations (such as authentication) and prepares the client. This function will be called automatically when any class method is called for the first time, but if you need to initialize it before calling an actual method, feel free to call initialize() directly.

You can await on this method if you want to make sure the client is initialized.

Returns
TypeDescription
Promise<{ [name: string]: Function; }>

{Promise} A promise that resolves to an authenticated service stub.