-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDrivingRoute.jsx
130 lines (108 loc) · 3.01 KB
/
DrivingRoute.jsx
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
import React from 'react';
import PropTypes from 'prop-types';
export default class DrivingRoute extends React.PureComponent {
static propTypes = {
// 起点
start: PropTypes.shape({
lng: PropTypes.number,
lat: PropTypes.number,
}),
// 终点
end: PropTypes.shape({
lng: PropTypes.number,
lat: PropTypes.number,
}),
// 途径点 [Point, Point, Point]
waypoints: PropTypes.array,
onSearchSuccess: PropTypes.func,
onSearchFail: PropTypes.func,
}
/**
* 设置默认的props属性
*/
static defaultProps = {
start: '',
end: '',
waypoints: [],
onSearchSuccess: (info) => { console.log(`获取驾驶路线成功${JSON.stringify(info)}`); },
onSearchFail: (msg) => { console.log(msg); },
}
componentDidUpdate() {
this.initialize();
}
componentDidMount() {
this.initialize();
}
componentWillUnmount() {
this.destroy();
}
destroy() {
this.driving && this.driving.clearResults();
this.driving = null;
}
initialize() {
const self = this;
const { map, onSearchSuccess, onSearchFail, start, waypoints, end } = this.props;
if (!map || !(start && end)) {
return;
}
this.destroy();
// clear all overlays in map and resets it
if (map.getOverlays().length) {
map.clearOverlays();
// map.reset();
}
const driveInfomation = {
distance: 0,
time: 0
};
this.driving = new window.BMap.DrivingRoute(map, {
renderOptions: {
map,
policy: this.props.policy || window.BMAP_DRIVING_POLICY_LEAST_TIME,
autoViewport: true,
},
onSearchComplete: function onComplete(results) {
if (!self.driving || !self.driving.getStatus) return;
if (self.driving && self.driving.getStatus() !== window.BMAP_STATUS_SUCCESS) {
return onSearchFail('获取行车路线失败!');
}
const plan = results.getPlan(0);
driveInfomation.time = plan.getDuration(true);
driveInfomation.distance = plan.getDistance(true);
onSearchSuccess(driveInfomation);
},
});
let startPoint = '';
let endPoint = '';
let wayPoints = [];
if (start.lng && start.lat) {
startPoint = new window.BMap.Point(start.lng, start.lat);
}
if (waypoints && waypoints.length) {
waypoints.forEach((point, index) => {
if (point && typeof point === 'object' && point.lng && point.lat) {
wayPoints[index] = new window.BMap.Point(point.lng, point.lat);
} else {
wayPoints[index] = null;
}
});
}
if (end.lng && end.lat) {
endPoint = new window.BMap.Point(end.lng, end.lat);
}
if (
startPoint &&
endPoint &&
(startPoint instanceof window.BMap.Point) &&
(endPoint instanceof window.BMap.Point)
) {
this.driving.search(startPoint, endPoint, {
waypoints: wayPoints.filter(t => t && t instanceof window.BMap.Point),
});
}
}
render() {
return null;
}
}