google-maps-services-js icon indicating copy to clipboard operation
google-maps-services-js copied to clipboard

directions: Type error of via_waypoint field inside routeLeg

Open kataiga opened this issue 2 years ago • 1 comments

Hello,

When I try to use the via_waypoint field of the DirectionsLeg (that is called routeLeg inside your code for some reason) I got this error Property 'via_waypoint' does not exist on type 'RouteLeg'.ts(2339 but the data is present in the response and your documentation

an other thing that is wierd in the doc and may be related, it is said for the Directions.legs:

A separate leg will be present for each waypoint or destination specified.

But this is not the case actually if I send 0, 1, 13 waypoint i only got one leg what is the reel expected behavior ?


Environment details

NodeJS v16.14 npm package @googlemaps/google-maps-services-js v3.3.16

Steps to reproduce

  1. call the directions api with a waypoint
  2. try to use directions.data.routes[i].legs[i].via_waypoint
  3. you will got typescript error

Code example

import { Client } from '@googlemaps/google-maps-services-js';

const key = process.env.GOOGLE_API_KEY || '';
const client = new Client({});

export const getDistanceMatrix = (addresses: string[], departureTime?: Date): Promise<void> => client
  .directions({
    // eslint-disable-next-line @typescript-eslint/ban-ts-comment
    // @ts-ignore: TS2322
    params: {
      key,
      origin: addresses[0],
      destination: addresses[addresses.length - 1],
      waypoints: addresses.slice(1, -1).map((address) => `via:${address}`),
      ...departureTime
        ? {
          traffic_model: 'best_guess',
          departure_time: departureTime,
        }
        : {},
      alternatives: true,
    },
  })
  .then((directions) => {
    
    console.log(directions.data.routes[0].legs[0].via_waypoint)
})

const adresses = [
  "25 Rue de la Marbrerie, Lille, France",
  "18 Rue Jules Guesde, 59650 Villeneuve-d'Ascq, France",
  "Ecole Elémentaire Voltaire-Sévigné, Rue Emile Zola Lomme, Lille, France"    
]

getDistanceMatrix(adresses)

kataiga avatar Dec 21 '22 12:12 kataiga

@kataiga Thank you for opening this issue. 🙏 Please check out these other resources that might help you get to a resolution in the meantime:

This is an automated message, feel free to ignore.

wangela avatar Dec 21 '22 12:12 wangela