-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
127 lines (104 loc) · 2.88 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
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
var Emitter = require('emitter');
var metric = require('spherical').distance;
var distance = require('distance');
module.exports = directions;
var margin = {
line: 20, // max distance from the line segment for which we consider tracking this line
point: 10 // max distance from the point in which we consider that we arrived
};
function pos2ll(pos) {
return [pos.coords.longitude, pos.coords.latitude]; // x,y
}
// return index of the closest leg (only if < margin)
function find(pos, legs) {
var result, ll = pos2ll(pos);
// find closest point on the polyline
result = legs.reduce(function(r, leg, index) {
var closest, distance_2;
closest = distance.closest(ll, leg.from, leg.to);
distance_2 = distance.p2p_2(ll, closest);
if (r.distance_2 === undefined || r.distance_2 > distance_2) {
// found closer leg
r.distance_2 = distance_2;
r.closest = closest;
r.index = index;
}
return r;
}, {});
// check if it's close enough
if (metric(ll, result.closest) < margin.line) {
return result.index;
}
}
// decide if we are still on tracked leg or if we should move on to the next one
function detect(pos, legs, index) {
var ll = pos2ll(pos),
current = legs[index],
candidate = legs[index + 1],
closest;
if (candidate) {
closest = distance.closest(ll, candidate.from, candidate.to);
if (metric(ll, closest) < margin.point) {
return index + 1; // next one is close enough
}
}
closest = distance.closest(ll, current.from, current.to);
if (metric(ll, closest) < margin.line) {
return index; // still following current
}
}
// emits the following events
// turn, follow, lost
function directions(legs) {
var self,
my = {
legs: legs
};
function distanceToNextTurn(pos, leg) {
return metric(pos2ll(pos), leg.to);
}
function emitFollow(pos, index) {
var leg = my.legs[index];
self.emit('follow', leg, distanceToNextTurn(pos, leg));
}
function emitTurn(pos, index) {
var next = my.legs[index + 1];
self.emit('turn', my.legs[index], next, distanceToNextTurn(pos, next));
}
function start(pos) {
var current = find(pos, my.legs);
if (typeof current === 'number') {
emitFollow(pos, current);
} else {
self.emit('lost');
}
return current;
}
function track(pos) {
var current = detect(pos, my.legs, my.current);
if (current === my.current) {
emitFollow(pos, my.current);
} else if (typeof current === 'number') {
emitTurn(pos, my.current);
} else {
self.emit('lost');
}
return current;
}
function end() {
my.current = undefined;
}
function update(position) {
if (typeof my.current !== 'number') {
my.current = start(position);
} else {
my.current = track(position);
}
return self;
}
self = {
update: update,
end: end
};
return Emitter(self);
}