-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMapViewDirections.js
More file actions
158 lines (131 loc) · 3.69 KB
/
Copy pathMapViewDirections.js
File metadata and controls
158 lines (131 loc) · 3.69 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import React, { Component } from 'react';
import PropTypes from 'prop-types';
// import MapView from 'react-native-maps';
import { MapView } from 'expo';
class MapViewDirections extends Component {
constructor(props) {
super(props);
this.state = {
coordinates: null,
distance: null,
duration: null,
};
}
componentDidMount() {
this.fetchAndRenderRoute();
}
componentWillReceiveProps(nextProps) {
if ((nextProps.origin != this.props.origin) || (nextProps.destination != this.props.destination)) {
this.resetState(this.fetchAndRenderRoute);
}
}
resetState = (cb = null) => {
this.setState({
coordinates: null,
distance: null,
duration: null,
}, cb);
}
decode(t, e) {
for (var n, o, u = 0, l = 0, r = 0, d = [], h = 0, i = 0, a = null, c = Math.pow(10, e || 5); u < t.length;) {
a = null, h = 0, i = 0;
do a = t.charCodeAt(u++) - 63, i |= (31 & a) << h, h += 5; while (a >= 32);
n = 1 & i ? ~(i >> 1) : i >> 1, h = i = 0;
do a = t.charCodeAt(u++) - 63, i |= (31 & a) << h, h += 5; while (a >= 32);
o = 1 & i ? ~(i >> 1) : i >> 1, l += n, r += o, d.push([l / c, r / c])
}
return d = d.map(function(t) {
return {
latitude: t[0],
longitude: t[1]
}
});
}
fetchAndRenderRoute = () => {
let {
origin,
destination,
apikey,
onReady,
onError,
} = this.props;
if (origin.latitude && origin.longitude) {
origin = `${origin.latitude},${origin.longitude}`;
}
if (destination.latitude && destination.longitude) {
destination = `${destination.latitude},${destination.longitude}`;
}
this.fetchRoute(origin, destination, apikey)
.then(result => {
this.setState(result);
onReady && onReady(result);
})
.catch(errorMessage => {
this.resetState();
console.warn(`MapViewDirections Error: ${errorMessage}`); // eslint-disable-line no-console
onError && onError(errorMessage);
});
}
fetchRoute = (origin, destination, apikey) => {
const mode = 'driving';
const url = `https://maps.googleapis.com/maps/api/directions/json?origin=${origin}&destination=${destination}&key=${apikey}&mode=${mode}`;
return fetch(url)
.then(response => response.json())
.then(json => {
if (json.status !== 'OK') {
const errorMessage = json.error_message || 'Unknown error';
return Promise.reject(errorMessage);
}
if (json.routes.length) {
const route = json.routes[0];
return Promise.resolve({
distance: route.legs.reduce((carry, curr) => {
return carry + curr.distance.value;
}, 0) / 1000,
duration: route.legs.reduce((carry, curr) => {
return carry + curr.duration.value;
}, 0) / 60,
coordinates: this.decode(route.overview_polyline.points)
});
} else {
return Promise.reject();
}
});
}
render() {
if (!this.state.coordinates) {
return null;
}
const {
origin, // eslint-disable-line no-unused-vars
destination, // eslint-disable-line no-unused-vars
apikey, // eslint-disable-line no-unused-vars
onReady, // eslint-disable-line no-unused-vars
onError, // eslint-disable-line no-unused-vars
...props
} = this.props;
return (
<MapView.Polyline coordinates={this.state.coordinates} {...props} />
);
}
}
MapViewDirections.propTypes = {
origin: PropTypes.oneOfType([
PropTypes.string,
PropTypes.shape({
latitude: PropTypes.number.isRequired,
longitude: PropTypes.number.isRequired,
}),
]),
destination: PropTypes.oneOfType([
PropTypes.string,
PropTypes.shape({
latitude: PropTypes.number.isRequired,
longitude: PropTypes.number.isRequired,
}),
]),
apikey: PropTypes.string.isRequired,
onReady: PropTypes.func,
onError: PropTypes.func,
};
export default MapViewDirections;