Support beelines in hash url (first stab)

This commit is contained in:
Norbert Renner 2021-06-19 16:23:01 +02:00
parent f3d48dc63e
commit 3c8be96085
5 changed files with 50 additions and 10 deletions

View file

@ -74,7 +74,15 @@ BR.Export = L.Class.extend({
link.download = (name || 'brouter') + '.' + format;
link.click();
} else {
var uri = this.router.getUrl(this.latLngs, this.pois.getMarkers(), null, format, nameUri, includeWaypoints);
var uri = this.router.getUrl(
this.latLngs,
null,
this.pois.getMarkers(),
null,
format,
nameUri,
includeWaypoints
);
var evt = document.createEvent('MouseEvents');
evt.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
var link = document.createElement('a');

View file

@ -432,7 +432,7 @@
if (opts.lonlats) {
routing.draw(false);
routing.clear();
routing.setWaypoints(opts.lonlats);
routing.setWaypoints(opts.lonlats, opts.beelineFlags);
}
if (opts.pois) {
pois.setMarkers(opts.pois);
@ -455,7 +455,13 @@
// this callback is used to append anything in URL after L.Hash wrote #map=zoom/lat/lng/layer
urlHash.additionalCb = function () {
var url = router
.getUrl(routing.getWaypoints(), pois.getMarkers(), circlego ? circlego.getCircle() : null, null)
.getUrl(
routing.getWaypoints(),
routing.getBeelineFlags(),
pois.getMarkers(),
circlego ? circlego.getCircle() : null,
null
)
.substr('brouter?'.length + 1);
// by default brouter use | as separator. To make URL more human-readable, we remplace them with ; for users

View file

@ -341,7 +341,7 @@ BR.routeLoader = function (map, layersControl, routing, pois) {
}
if (routingPoints.length > 0) {
routing.setWaypoints(routingPoints, function (event) {
routing.setWaypoints(routingPoints, null, function (event) {
if (!event) return;
var err = event.error;
BR.message.showError(

View file

@ -290,7 +290,7 @@ BR.Routing = L.Routing.extend({
}
},
setWaypoints: function (latLngs, cb) {
setWaypoints: function (latLngs, beelineFlags, cb) {
var i;
var callbackCount = 0;
var firstErr;
@ -319,7 +319,8 @@ BR.Routing = L.Routing.extend({
this._loadingTrailerGroup._map = null;
for (i = 0; latLngs && i < latLngs.length; i++) {
this.addWaypoint(latLngs[i], this._waypoints._last, null, callback);
const beeline = beelineFlags && i < beelineFlags.length ? beelineFlags[i] : null;
this.addWaypoint(latLngs[i], beeline, this._waypoints._last, null, callback);
}
this._loadingTrailerGroup._map = this._map;

View file

@ -42,10 +42,15 @@ L.BRouter = L.Class.extend({
L.setOptions(this, options);
},
getUrlParams: function (latLngs, pois, circlego, format) {
getUrlParams: function (latLngs, beelineFlags, pois, circlego, format) {
params = {};
if (this._getLonLatsString(latLngs) != null) params.lonlats = this._getLonLatsString(latLngs);
if (beelineFlags && beelineFlags.length > 0) {
const beelineString = this._getBeelineString(beelineFlags);
if (beelineString.length > 0) params.straight = beelineString;
}
if (this.options.nogos && this._getNogosString(this.options.nogos).length > 0)
params.nogos = this._getNogosString(this.options.nogos);
@ -84,6 +89,9 @@ L.BRouter = L.Class.extend({
if (params.lonlats) {
opts.lonlats = this._parseLonLats(params.lonlats);
}
if (params.straight) {
opts.beelineFlags = this._parseBeelines(params.straight);
}
if (params.nogos) {
opts.nogos = this._parseNogos(params.nogos);
}
@ -117,11 +125,12 @@ L.BRouter = L.Class.extend({
return opts;
},
getUrl: function (latLngs, pois, circlego, format, trackname, exportWaypoints) {
var urlParams = this.getUrlParams(latLngs, pois, circlego, format);
getUrl: function (latLngs, beelineFlags, pois, circlego, format, trackname, exportWaypoints) {
var urlParams = this.getUrlParams(latLngs, beelineFlags, pois, circlego, format);
var args = [];
if (urlParams.lonlats != null && urlParams.lonlats.length > 0)
args.push(L.Util.template('lonlats={lonlats}', urlParams));
if (urlParams.straight != null) args.push(L.Util.template('straight={straight}', urlParams));
if (urlParams.pois != null && urlParams.pois.length > 0) args.push(L.Util.template('pois={pois}', urlParams));
if (urlParams.circlego != null) args.push(L.Util.template('ringgo={circlego}', urlParams));
if (urlParams.nogos != null) args.push(L.Util.template('nogos={nogos}', urlParams));
@ -144,7 +153,7 @@ L.BRouter = L.Class.extend({
},
getRoute: function (latLngs, cb) {
var url = this.getUrl(latLngs, null, null, 'geojson'),
var url = this.getUrl(latLngs, null, null, null, 'geojson'),
xhr = new XMLHttpRequest();
if (!url) {
@ -305,6 +314,22 @@ L.BRouter = L.Class.extend({
return lonlats;
},
_getBeelineString: function (beelineFlags) {
var s = '';
for (var i = 0; i < beelineFlags.length; i++) {
s += beelineFlags[i] ? '1' : '0';
}
return s;
},
_parseBeelines: function (s) {
const beelineFlags = [];
for (const c of s) {
beelineFlags.push(!!+c);
}
return beelineFlags;
},
_getLonLatsNameString: function (latLngNames) {
var s = '';
for (var i = 0; i < latLngNames.length; i++) {