forked from zendesk/linksf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdistance.js
More file actions
41 lines (35 loc) · 1.48 KB
/
Copy pathdistance.js
File metadata and controls
41 lines (35 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
function fetchDistanceAndDuration(origins, destinations) {
const googleService = new google.maps.DistanceMatrixService()
// const promiseGoogleService = Promise.promisifyAll(, { promisifier: noErrPromisifier })
const matrixParams = {
origins,
destinations: destinations,
travelMode: google.maps.TravelMode.WALKING,
durationInTraffic: true,
avoidHighways: true,
avoidTolls: false,
unitSystem: google.maps.UnitSystem.IMPERIAL,
}
return new Promise((resolve, reject) => {
// Put all your code here, this section is throw-safe.
googleService.getDistanceMatrix(matrixParams, (response, status) => (
status == google.maps.DistanceMatrixStatus.OK ? resolve(response) :
reject(status)))
})
}
export function calculateDistance(location, currentLocation) {
const pos = new google.maps.LatLng(location.latitude, location.longitude)
const { latitude, longitude } = currentLocation.coords
const googleLocation = new google.maps.LatLng(latitude, longitude)
return fetchDistanceAndDuration([googleLocation], [pos])
}
export function calculateAllDistances(locations, currentLocation) {
const { latitude, longitude } = currentLocation.coords
const origins = [new google.maps.LatLng(latitude, longitude)]
const destinations = locations.map(location => (
new google.maps.LatLng(
location.latitude,
location.longitude
)))
return fetchDistanceAndDuration(origins, destinations)
}