Skip to content

Commit 38de4cf

Browse files
committed
Fixes waypoint index
Fix mapbox#184
1 parent 4a6d689 commit 38de4cf

File tree

2 files changed

+63
-5
lines changed

2 files changed

+63
-5
lines changed

src/directions.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ export default class MapboxDirections {
9898
* @returns {Control} `this`
9999
*/
100100
onRemove(map) {
101-
this.container.parentNode.removeChild(this.container);
101+
if (this.container.parentNode !== null) {
102+
this.container.parentNode.removeChild(this.container);
103+
}
102104
this.removeRoutes();
103105
map.off('mousedown', this.onDragDown);
104106
map.off('mousemove', this.move);
@@ -363,7 +365,7 @@ export default class MapboxDirections {
363365
_onDragUp() {
364366
if (!this.isDragging) return;
365367

366-
const { hoverMarker, origin, destination } = store.getState();
368+
const { hoverMarker, origin, destination, waypoints } = store.getState();
367369

368370
switch (this.isDragging.layer.id) {
369371
case 'directions-origin-point':
@@ -375,7 +377,9 @@ export default class MapboxDirections {
375377
case 'directions-hover-point':
376378
// Add waypoint if a sufficent amount of dragging has occurred.
377379
if (hoverMarker.geometry && !utils.coordinateMatch(this.isDragging, hoverMarker)) {
378-
this.actions.addWaypoint(0, hoverMarker);
380+
const click = this.isDragging.geometry.coordinates;
381+
const index = utils.getNextWaypoint(this.getRoute.bind(this), waypoints, click);
382+
this.actions.addWaypoint(index, hoverMarker);
379383
}
380384
break;
381385
}
@@ -520,7 +524,22 @@ export default class MapboxDirections {
520524
getWaypoints() {
521525
return store.getState().waypoints;
522526
}
523-
527+
528+
/**
529+
* Fetch all current points in a route.
530+
* @returns {Array} route points
531+
*/
532+
getRoute() {
533+
return this
534+
._map
535+
.getSource('directions')
536+
._data
537+
.features
538+
.find(({geometry}) => geometry.type === 'LineString')
539+
.geometry
540+
.coordinates;
541+
}
542+
524543
/**
525544
* Removes all routes and waypoints from the map.
526545
*

src/utils.js

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,43 @@ const format = {
6262
}
6363
};
6464

65-
export default { format, coordinateMatch, createPoint, validCoords, wrap, roundWithOriginalPrecision };
65+
const distance = ([x1, y1], [x2, y2]) =>
66+
Math.hypot((x1 - x2), (y1 - y2));
67+
68+
const nearest = origin =>
69+
(p1, p2, index) =>
70+
(distance(origin, p1) < distance(origin, p2))
71+
? p1
72+
: p2.concat(index);
73+
74+
75+
const find = (route, origin) => route
76+
.reduce(nearest(origin), [Infinity, Infinity, -1])[2];
77+
78+
const compare = point =>
79+
waypoint =>
80+
coordinateMatch(point, waypoint);
81+
82+
const getNextWaypoint = (getRoute, waypoints, origin) => {
83+
if (waypoints.length === 0) return 0;
84+
85+
const route = getRoute();
86+
87+
for(let i = find(route, origin); i < route.length; i++) {
88+
const index = waypoints.findIndex(compare({ geometry: { coordinates: route[i] } }));
89+
if (index !== -1) {
90+
return index;
91+
}
92+
}
93+
return waypoints.length;
94+
};
95+
96+
export default {
97+
format,
98+
coordinateMatch,
99+
createPoint,
100+
validCoords,
101+
wrap,
102+
roundWithOriginalPrecision,
103+
getNextWaypoint
104+
};

0 commit comments

Comments
 (0)