-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
55 lines (45 loc) · 1.38 KB
/
index.js
File metadata and controls
55 lines (45 loc) · 1.38 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { Linking } from 'react-native'
const isValidLatLng = (num, range) => typeof num === 'number' && num <= range && num >= -1 * range
const isValidCoordinates = coords =>
isValidLatLng(coords.latitude, 90) && isValidLatLng(coords.longitude, 180)
const getParams = (params = []) => {
return params
.map(({ key, value }) => {
return `${key}=${value}`;
})
.join('&');
};
const getWaypoints = (waypoints = []) => {
if (waypoints.length === 0) {
return ''
}
const params = waypoints
.map(value => `${value.latitude},${value.longitude}`)
.join('|')
return `&avoid=tolls|highways&waypoints=${params}`
}
function getDirections ({ destination, source, params = [], waypoints = [] } = {}) {
if (destination && isValidCoordinates(destination)) {
params.push({
key: 'destination',
value: `${destination.latitude},${destination.longitude}`
})
}
if (source && isValidCoordinates(source)) {
params.push({
key: 'origin',
value: `${source.latitude},${source.longitude}`
})
}
const url = `https://www.google.com/maps/dir/?api=1&${getParams(
params
)}${getWaypoints(waypoints)}`
return Linking.canOpenURL(url).then(supported => {
if (!supported) {
return Promise.reject(new Error(`Could not open the url: ${url}`))
} else {
return Linking.openURL(url)
}
})
}
export default getDirections