Skip to content

Instantly share code, notes, and snippets.

@andrey-hohlov
Created January 24, 2019 11:51
Show Gist options
  • Save andrey-hohlov/28fcfe8fd91e744ff38d848e21b2c5bb to your computer and use it in GitHub Desktop.
Save andrey-hohlov/28fcfe8fd91e744ff38d848e21b2c5bb to your computer and use it in GitHub Desktop.
import axios from 'axios';
import constants from '@/core/common/constants';
export function fetchLegDistance(leg) {
return new Promise((resolve, reject) => {
if (!leg.startLocation.address.verified || !leg.startLocation.address.verified) {
reject(new Error('Unverified leg address'));
return;
}
const coords = ['startLocation', 'endLocation'].map((key) => {
const { lng, lat } = leg[key].address.location;
return [lng, lat].join(',');
}).join(';');
axios.get(`https://api.mapbox.com/directions/v5/mapbox/driving/${coords}`, {
params: {
access_token: process.env.MAPBOX_ACCESS_TOKEN,
},
})
.then(({ data }) => {
if (data.code !== 'Ok' && !data.routes[0]) {
reject(new Error('Invalid Mapbox response'));
return;
}
resolve({
value: data.result.routes[0].distance,
uom: constants.distance.METER,
});
})
.catch((err) => {
reject(err);
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment