-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathdirections-service.js
81 lines (73 loc) · 2.58 KB
/
directions-service.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
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
function DirectionsService(options) {
this._options = options || {};
}
DirectionsService.prototype = {
constructor: DirectionsService,
route: function (request, callback) {
var self = this,
waypoints = [request.origin].concat(request.waypoints || [], [request.destination]),
geocoder = new MultiGeocoder();
geocoder.geocode(waypoints).then(function (res) {
ymaps.route(waypoints, self._options).then(function (route) {
self._updateWaypoints(route.getWayPoints(), res.geoObjects);
callback({ routes: [self._getRoute(route)] });
});
});
},
_getBounds: function (route) {
return route && route.properties.get('boundedBy') || null;
},
_updateWaypoints: function (waypoints, source) {
waypoints.each(function (waypoint, i) {
waypoint.properties.set(
source.get(i).properties.getAll()
);
});
},
_getLegs: function (route) {
var waypoints = route.getWayPoints(),
legs = [];
route.getPaths().each(function (path, i) {
var start = waypoints.get(i),
end = waypoints.get(i + 1);
legs.push(this._createLeg(start, end, path));
}, this);
return legs;
},
_createLeg: function (start, end, path) {
return {
distance: path.getLength(),
duration: this._getDuration(path),
start_address: start.properties.get('text'),
end_address: end.properties.get('text'),
start_location: start.geometry.getCoordinates(),
end_location: end.geometry.getCoordinates(),
steps: this._getSteps(path.getSegments())
};
},
_getSteps: function (segments) {
return segments.map(this._createStep, this);
},
_createStep: function (segment) {
var path = segment.getCoordinates();
return {
distance: segment.getLength(),
duration: this._getDuration(segment),
instructions: segment.getHumanAction(),
path: path,
street: segment.getStreet(),
start_location: path[0],
end_location: path[path.length - 1]
};
},
_getDuration: function (segment) {
var avoidTrafficJams = this._options.avoidTrafficJams;
return avoidTrafficJams ? segment.getJamsTime() : segment.getTime();
},
_getRoute: function (route) {
return {
bounds: this._getBounds(route),
legs: this._getLegs(route)
};
}
};