maps_distance_matrix
Calculate travel distances and times between multiple origins and destinations using driving, walking, bicycling, or transit modes for efficient route planning.
Instructions
計算多個起點和終點之間的距離和時間
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| destinations | Yes | 終點地址或座標列表 | |
| mode | No | 交通模式 | driving |
| origins | Yes | 起點地址或座標列表 |
Implementation Reference
- src/tools/maps/distanceMatrix.ts:16-46 (handler)The ACTION function implements the core logic of the 'maps_distance_matrix' tool. It creates a PlacesSearcher instance, calls calculateDistanceMatrix with the provided origins, destinations, and mode, and returns the formatted result or error.async function ACTION(params: any): Promise<{ content: any[]; isError?: boolean }> { try { // Create a new PlacesSearcher instance with the current request's API key const apiKey = getCurrentApiKey(); const placesSearcher = new PlacesSearcher(apiKey); const result = await placesSearcher.calculateDistanceMatrix(params.origins, params.destinations, params.mode); if (!result.success) { return { content: [{ type: "text", text: result.error || "Failed to calculate distance matrix" }], isError: true, }; } return { content: [ { type: "text", text: JSON.stringify(result.data, null, 2), }, ], isError: false, }; } catch (error: any) { const errorMessage = error instanceof Error ? error.message : JSON.stringify(error); return { isError: true, content: [{ type: "text", text: `Error calculating distance matrix: ${errorMessage}` }], }; } }
- Zod schema defining the input parameters for the tool: origins (array of strings), destinations (array of strings), and mode (enum with default 'driving').const SCHEMA = { origins: z.array(z.string()).describe("List of origin addresses or coordinates"), destinations: z.array(z.string()).describe("List of destination addresses or coordinates"), mode: z.enum(["driving", "walking", "bicycling", "transit"]).default("driving").describe("Travel mode for calculation"), };
- src/config.ts:47-52 (registration)Tool registration in the server configuration, importing and defining the tool using NAME, DESCRIPTION, SCHEMA, and ACTION from DistanceMatrix export.{ name: DistanceMatrix.NAME, description: DistanceMatrix.DESCRIPTION, schema: DistanceMatrix.SCHEMA, action: (params: DistanceMatrixParams) => DistanceMatrix.ACTION(params), },
- Supporting method in PlacesSearcher class that wraps the low-level Google Maps distance matrix call and handles errors.async calculateDistanceMatrix(origins: string[], destinations: string[], mode: "driving" | "walking" | "bicycling" | "transit" = "driving"): Promise<DistanceMatrixResponse> { try { const result = await this.mapsTools.calculateDistanceMatrix(origins, destinations, mode); return { success: true, data: result, }; } catch (error) { return { success: false, error: error instanceof Error ? error.message : "An error occurred while calculating distance matrix", }; } }