0

i am using this google services js api and I am not sure why this code works for individual lat/lang?

 const { data } = await GoogleClient.distancematrix({
              params: {
                origins: [[userLocation.latitude, userLocation.longitude]],
                destinations: [
                  [
                    node.merchant.geometry.coordinates[0],
                    node.merchant.geometry.coordinates[1],
                  ] as LatLng,
                ],
                key: GOOGLE_API_KEY,
                units: UnitSystem.imperial,
              },
            });

what i wanted to do is pass multiple origins and destinations in 1 call:

  const destinations = data.nearbyCampaigns.edges.map(
          ({ node }) =>
            [node.merchant.geometry.coordinates[0], node.merchant.geometry.coordinates[1]] as LatLng
        );
        //e.g.: destinations =  [[8.532997603846151,124.34326170144848],[8.542997603846151,124.34326170144848],[8.552997603846151,124.34326170144848]]
        const origins = destinations.map(
          (_) => [userLocation.latitude, userLocation.longitude] as LatLng
        );
        //e.g.: origins = [[8.55381,124.523956],[8.54981,124.523956],[8.55981,124.523956]]
        const { data: data1 } = await GoogleClient.distancematrix({
          params: {
            origins: [...origins],
            destinations: [...destinations],
            key: GOOGLE_API_KEY,
            units: UnitSystem.imperial,
          },
        });

but doing that i will get this error:

{"destination_addresses":["","",""],"origin_addresses":["","",""],"rows":[{"elements":[{"status":"NOT_FOUND"},{"status":"NOT_FOUND"},{"status":"NOT_FOUND"}]},{"elements":[{"status":"NOT_FOUND"},{"status":"NOT_FOUND"},{"status":"NOT_FOUND"}]},{"elements":[{"status":"NOT_FOUND"},{"status":"NOT_FOUND"},{"status":"NOT_FOUND"}]}],"status":"OK"}

1 Answer 1

0

Here's an easy way on how to pass multiple origin and destination using Node.js Client for Google Maps Services. I base my code on how the github document shows how the Google Maps API should be called. I used your origin and destinations coordinates and put them in a json file.

Main.js

let origin = require("./origin.json")
let dest = require("./destination.json")

let originArray = origin.geometry;
let destArray = dest.geometry;

const {
    Client
} = require("@googlemaps/google-maps-services-js");
const client = new Client({});


client
    .distancematrix({
        params: {
            origins: originArray,
            destinations: destArray,
            key: "YOUR_API_KEY",
        }
    })
    .then((r) => {
        console.log(r.data);
    })
    .catch((e) => {
        console.log(e.response.data.error_message);
    });

origin.json

{"geometry":[
  {"lat":8.55381, "lng":124.523956},
  {"lat":88.54981, "lng":124.523956},
  {"lat":8.55981, "lng":124.523956}
]}

destination.json

{"geometry":[
  {"lat":8.532997603846151, "lng":124.34326170144848},
  {"lat":8.542997603846151, "lng":124.34326170144848},
  {"lat":8.552997603846151, "lng":124.34326170144848}
]}
``
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.