Created
January 24, 2019 11:51
-
-
Save andrey-hohlov/28fcfe8fd91e744ff38d848e21b2c5bb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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