Reputation: 19
I am trying to implement a google maps distance matrix api request using, node JS google Client library. specifically
@googlemaps/google-maps-services-js
The problem am facing, I can only finds source code example for elevation in the documentation.
const client = new Client({});
client
.elevation({
params: {
locations: [{ lat: 45, lng: -110 }],
key: "asdf",
},
timeout: 1000, // milliseconds
})
.then((r) => {
console.log(r.data.results[0].elevation);
})
.catch((e) => {
console.log(e.response.data.error_message);
});
I need source code example for Distance Matrix so I can implement point to point distance calculation within node JS, using the client library.
const client = new Client({});
client
.distanceMatrixrequest()
Upvotes: -2
Views: 594
Reputation: 87
client
.distancematrix({
params: {
key: '<YOUR_KEY>',
origins: ['Greenwich, England'],
destinations: ['Stockholm, Sweden'],
travelMode: 'DRIVING',
// Feel free to set more params
},
timeout: 1000, // milliseconds
})
.then((r) => {
console.log(r.data.rows[0]?.elements);
res.json(r.data);
})
.catch((e) => {
console.log(e.response.data.status);
});
Response comes in this format:
[
{
distance: { text: '1,880 km', value: 1880123 },
duration: { text: '20 hours 59 mins', value: 75548 },
status: 'OK'
}
]
Upvotes: 0