Skip to content

Instantly share code, notes, and snippets.

@perliedman
Created December 5, 2016 08:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save perliedman/6d19485ff0564720c13ab28e6f6e74c9 to your computer and use it in GitHub Desktop.
Save perliedman/6d19485ff0564720c13ab28e6f6e74c9 to your computer and use it in GitHub Desktop.
Round Robin Routing Control
var RoundRobinPlan = L.Routing.Plan.extend({
initialize: function(waypoints, options) {
L.Routing.Plan.prototype.initialize.call(this, waypoints, options);
},
dragNewWaypoint: function(e) {
var nWps = this._waypoints.length,
insertionIndex = e.afterIndex > nWps - 1 ? 0 : e.afterIndex + 1,
i,
marker,
lines = [];
function dragNewWp(e) {
var i;
marker.setLatLng(e.latlng);
for (i = 0; i < lines.length; i++) {
lines[i].spliceLatLngs(1, 1, e.latlng);
}
}
function releaseNewWp(e) {
var i;
this._map.off('mouseup', releaseNewWp, this);
this._map.off('mousemove', dragNewWp, this);
for (i = 0; i < lines.length; i++) {
this._map.removeLayer(lines[i]);
}
this.spliceWaypoints(insertionIndex, 0, e.latlng);
delete this._newWp;
}
marker = L.marker(e.latlng).addTo(this._map);
for (i = 0; i < this.options.dragStyles.length; i++) {
lines.push(L.polyline([
this._waypoints[(e.afterIndex) % nWps].latLng,
e.latlng,
this._waypoints[(e.afterIndex + 1) % nWps].latLng
], this.options.dragStyles[i]).addTo(this._map));
}
this._markers.splice(insertionIndex, 0, marker);
this._map.on('mousemove', dragNewWp, this);
this._map.on('mouseup', releaseNewWp, this);
},
});
var RoundRobinRouteControl = L.Routing.Control.extend({
initialize: function(options) {
L.Routing.Control.prototype.initialize.call(this, L.extend({}, options, {
plan: new RoundRobinPlan(options.waypoints, options)
}));
},
route: function() {
var wps,
roundRobin;
if (!this.getPlan().isReady()) {
return;
}
wps = this.getPlan().getWaypoints();
roundRobin = wps.concat(wps[0]);
L.Routing.Control.prototype.route.call(this, {
waypoints: roundRobin
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment