-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
48 lines (40 loc) · 1.75 KB
/
index.js
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
import { Linking, Platform } from 'react-native';
import isValidCoordinates from 'is-valid-coordinates';
const PLATFORM = Platform.OS;
export const OpenMapDirections = (frmCoord = null, toCoord, transportType) => new Promise((resolve, reject) => {
let _toCoord;
const _frmCoord = _checkParameters(frmCoord) !== null ? `?saddr=${_checkParameters(frmCoord)}` : '';
if (_checkParameters(toCoord) !== null) {
_toCoord = (_frmCoord.length == 0 ? '?' : '&') + `daddr=${_checkParameters(toCoord)}`
} else {
throw new Error('You need to pass a valid endpoint(number)')
};
const _transportType = _checkTransportParameter(transportType) !== null ? `&dirflg=${_checkTransportParameter(transportType)}` : '';
const url = `${PLATFORM === 'ios' ? `https://maps.apple.com/` : 'https://maps.google.com/'}${_frmCoord}${_toCoord}${_transportType}`;
_openApp(url).then(result => { resolve(result) });
});
const _openApp = (url) => new Promise((resolve, reject) => {
Linking.canOpenURL(url)
.then(res => {
Linking.openURL(url)
.then(result => {
resolve('opening app....');
}).catch(err => { reject('Cannot link app!!!'); })
}).catch(err => {
reject('Cannot open app!!!');
})
});
const _checkParameters = (param) => {
if (param === null || param === undefined || typeof param.latitude === 'string' || typeof param.longitude === 'string') { return null; }
if (isValidCoordinates.longitude(param.longitude) && isValidCoordinates.latitude(param.latitude)) {
return `${param.latitude},${param.longitude}`
}
return null;
}
const _checkTransportParameter = (param) => {
const _transportType = param.toLowerCase();
if (_transportType === 'd' || _transportType === 'w' || _transportType === 'r' || _transportType === 'b') {
return _transportType;
}
return 'w';
}