目录

Batch Route Computation

Use Cases

This capability quickly calculates distances and travel times between multiple departure places and destinations across different transportation modes, including driving, walking, and cycling, in batches.

API Description

The following table describes APIs for route planning, which are mainly provided by methods in the navi namespace. For more information about the APIs, please refer to API Reference.

API Description
getDrivingMatrix(params: DrivingMatrixParams): Promise<MatrixResult> Performs batch route computation for driving.
getDrivingMatrix(context: common.Context, params: DrivingMatrixParams): Promise<MatrixResult> Performs batch route computation for driving. The context can be passed.
getWalkingMatrix(params: MatrixParams): Promise<MatrixResult> Performs batch route computation for walking.
getWalkingMatrix(context: common.Context, params: MatrixParams): Promise<MatrixResult> Performs batch route computation for walking. The context can be passed.
getCyclingMatrix(params: MatrixParams): Promise<MatrixResult> Performs batch route computation for cycling.
getCyclingMatrix(context: common.Context, params: MatrixParams): Promise<MatrixResult> Performs batch route computation for cycling. The context can be passed.
DrivingMatrixParams Parameters for batch driving route computation.
MatrixParams Parameters for batch walking and cycling route computation.
MatrixResult Result of batch route computation.

Development Procedure

Import required modules.

import { navi } from '@kit.MapKit';
import { BusinessError } from '@kit.BasicServicesKit';

[h2]Batch Route Computation for Driving

This capability plans driving routes between multiple departure places and destinations based on their coordinates. This capability can:

To ensure performance and accuracy, the number of departure places multiplied by the number of destinations in a single request must be less than 100.

async testDrivingMatrix() {
  let params: navi.DrivingMatrixParams = {
    // Longitude and latitude of the departure place.
    origins: [{
      latitude: 31.9844,
      longitude: 118.766253
    }, {
      latitude: 31.9644,
      longitude: 118.746253
    }],
    // Longitude and latitude of the destination.
    destinations: [{
      latitude: 31.9344,
      longitude: 118.706253
    }],
    // Time estimation mode.
    trafficMode: 2,
    language: 'en'
  };
  try {
    const result = await navi.getDrivingMatrix(params);
    console.info(`Succeeded in getting driving matrix. result is ${JSON.stringify(result)}`);
  } catch (error) {
    const err: BusinessError = error as BusinessError;
    console.error(`Failed in getting driving matrix. Code is ${err.code}, message is ${err.message}`);
  }
}

[h2]Batch Route Computation for Walking

This capability plans walking routes between multiple departure places and destinations based on their coordinates. This capability can:

To ensure performance and accuracy, the number of departure places multiplied by the number of destinations in a single request must be less than 100.

async testWalkingMatrix() {
  let params: navi.MatrixParams = {
    // Longitude and latitude of the departure place.
    origins: [
      {
        latitude: 31.9844,
        longitude: 118.766253
      }, {
      latitude: 31.9644,
      longitude: 118.746253
    }],
    // Longitude and latitude of the destination.
    destinations: [{
      latitude: 31.9344,
      longitude: 118.706253
    }],
    language: 'en'
  };
  try {
    const result = await navi.getWalkingMatrix(params);
    console.info(`Succeeded in getting walking matrix. result is ${JSON.stringify(result)}`);
  } catch (error) {
    const err: BusinessError = error as BusinessError;
    console.error(`Failed in getting walking matrix. Code is ${err.code}, message is ${err.message}`);
  }
}

[h2]Batch Route Computation for Cycling

This capability plans cycling routes between multiple departure places and destinations based on their coordinates. This capability can:

To ensure performance and accuracy, the number of departure places multiplied by the number of destinations in a single request must be less than 100.

async testCyclingMatrix() {
  let params: navi.MatrixParams = {
    // Longitude and latitude of the departure place.
    origins: [{
      latitude: 31.9844,
      longitude: 118.766253
    }, {
      latitude: 31.9644,
      longitude: 118.746253
    }],
    // Longitude and latitude of the destination.
    destinations: [{
      latitude: 31.9344,
      longitude: 118.706253
    }],
    language: 'en'
  };
  try {
    const result = await navi.getCyclingMatrix(params);
    console.info(`Succeeded in getting cycling matrix. result is ${JSON.stringify(result)}`);
  } catch (error) {
    const err: BusinessError = error as BusinessError;
    console.error(`Failed in getting cycling matrix. Code is ${err.code}, message is ${err.message}`);
  }
}