Search code examples
node.jsgoogle-mapsgoogle-maps-api-3

How to use Google Maps Directions Service in NodeJS Backend Server


I'm able to use Google Maps Javascript Directions / Routing API easily on the frontend in browser like this:

const directionsService = new google.maps.DirectionsService();

directionsService.route({
  origin: {lat: 52, lng: 45},
  destination: {lat: 123, lng: 123},
  optimizeWaypoints: true,
  travelMode: 'DRIVING',
  drivingOptions: {
    trafficModel: 'optimistic'
  }
})
.then((response, status) => {
  console.log(response, 'google maps response');
});

And that works perfectly.

However I'm now trying to do a backend job in NodeJS and i'm having a lot of trouble.

I'm trying to use this module: https://www.npmjs.com/package/@google/maps

And trying to follow this documentation: https://googlemaps.github.io/google-maps-services-js/index.html

Unfortunately the documentation doesn't have much in the way of examples and I'm trying to connect the dots of doing the same request on the backend, this is what I tried:

import googleMapsClient from '@google/maps';

const mapsClient = googleMapsClient.createClient({
  key: process.env.GOOGLE_MAPS_API_KEY
})

mapsClient.directions.route({
  origin: {lat: 40.154570, lng: -111.573910},
  destination: {lat: 40.259250, lng: -111.698360},
  optimizeWaypoints: true,
  travelMode: 'DRIVING',
  drivingOptions: {
    trafficModel: 'optimistic',
  }
})
.then(response => {
  console.log(response, 'route response');
})

The error I get is the above code mapsClient.directions.route is not a function

When I try this:

mapsClient.geocode({
  address: '1600 Amphitheatre Parkway, Mountain View, CA'
}, function(err, response) {
  if(err) console.error(err.message, 'geocode error');
  if (!err) {
    console.log(response.json.results);
  }
});

That does work fine so at least i'm importing the module correctly, the documentation is just so confusing I'm not sure how to get the directions / routing service to work in the backend?


Solution

  • @google/maps is deprecated. You should use @googlemaps/google-maps-services-js. You can't follow the docs of the new client and use the old one.

    So first, you should remove the old library and install the new one:

    npm remove @google/maps
    npm install @googlemaps/google-maps-services-js
    

    There is no route() function (as the error message says).

    Directions takes a DirectionsRequest so try this way:

    import {Client} from "@googlemaps/google-maps-services-js";
    
    const {Client} = require("@googlemaps/google-maps-services-js");
    
    const client = new Client({});
    
    client.directions({
        params: {
          origin: origin,
          destination: destination,
          mode: 'driving',
          key: process.env.GOOGLE_MAPS_API_KEY
          // Any other params here
        },
        timeout: 1000, // milliseconds
      })
      .then(response => {
        console.log(response, 'route response');
      })
    

    This should work. If it doesn't, please share further debugging information.