Skip to content

Commit

Permalink
handle URL
Browse files Browse the repository at this point in the history
  • Loading branch information
bagage committed Sep 2, 2019
1 parent e0ae6da commit 5ec4cbe
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 29 deletions.
8 changes: 7 additions & 1 deletion js/control/Export.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ BR.Export = L.Class.extend({
var name = encodeURIComponent(exportForm['trackname'].value);
var includeWaypoints = exportForm['include-waypoints'].checked;

var uri = this.router.getUrl(this.latLngs, format, name, includeWaypoints);
var uri = this.router.getUrl(
this.latLngs,
null /*fixme: add checkbox and use poi if ticked*/,
format,
name,
includeWaypoints
);

var evt = document.createEvent('MouseEvents');
evt.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
Expand Down
13 changes: 10 additions & 3 deletions js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,6 @@
trackMessages.update(track, segments);

exportRoute.update(latLngs);

console.log('markers are', poi.getPoiMarkers());
}

routing.addTo(map);
Expand Down Expand Up @@ -305,6 +303,7 @@
return p;
};
if (url == null) return;

var opts = router.parseUrlParams(url2params(url));
router.setOptions(opts);
routingOptions.setOptions(opts);
Expand All @@ -316,6 +315,10 @@
routing.clear();
routing.setWaypoints(opts.lonlats);
}

if (opts.poi) {
poi.setMarkers(opts.poi);
}
};

var onInvalidHashChangeCb = function(params) {
Expand All @@ -328,9 +331,13 @@

// do not initialize immediately
urlHash = new L.Hash(null, null);
// 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(), null).substr('brouter?'.length + 1);
var url = router.getUrl(routing.getWaypoints(), poi.getMarkers(), null).substr('brouter?'.length + 1);

// by default brouter use | as separator. To make URL more human-readable, we remplace them with ; for users
url = url.replace(/\|/g, ';');

return url.length > 0 ? '&' + url : null;
};
urlHash.onHashChangeCb = onHashChangeCb;
Expand Down
55 changes: 34 additions & 21 deletions js/plugin/POIMarkers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ BR.poiMarkers = L.Control.extend({
},

onAdd: function(map) {
var thiz = this;
var self = this;

this.map = map;
this.markersLayer = L.layerGroup([]);
Expand All @@ -17,8 +17,8 @@ BR.poiMarkers = L.Control.extend({
icon: 'fa-hand-o-right',
onClick: function(control) {
control.state('deactivate-poi');
map.on('click', thiz.onMapClick, thiz);
thiz.options.routing.draw(false);
map.on('click', self.onMapClick, self);
self.options.routing.draw(false);
L.DomUtil.addClass(map.getContainer(), 'routing-draw-enabled');
},
title: i18next.t('map.draw-poi-start')
Expand All @@ -28,7 +28,7 @@ BR.poiMarkers = L.Control.extend({
icon: 'fa-hand-o-right active',
onClick: function(control) {
control.state('activate-poi');
map.off('click', thiz.onMapClick, thiz);
map.off('click', self.onMapClick, self);
L.DomUtil.removeClass(map.getContainer(), 'routing-draw-enabled');
},
title: i18next.t('map.draw-poi-stop')
Expand All @@ -42,32 +42,45 @@ BR.poiMarkers = L.Control.extend({
},

onMapClick: function(e) {
var markersLayer = this.markersLayer;
var self = this;
bootbox.prompt({
size: 'small',
title: i18next.t('map.enter-poi-name'),
callback: function(result) {
if (result !== null) {
var icon = L.AwesomeMarkers.icon({
icon: 'star',
markerColor: 'red'
});
L.marker(e.latlng, { icon: icon })
.bindPopup(result)
.addTo(markersLayer);
self.addMarker(e.latlng, result);
}
}
});
},

getPoiMarkers: function() {
return this.markersLayer
.getLayers()
.map(it => {
var latlng = it._latlng;
var name = it._popup._content;
return latlng + ',' + name;
})
.join('|');
addMarker: function(latlng, name) {
var icon = L.AwesomeMarkers.icon({
icon: 'star',
markerColor: 'red'
});
L.marker(latlng, { icon: icon, draggable: true })
.bindPopup(name)
.addTo(this.markersLayer);
},

setMarkers: function(latLngNames) {
this.markersLayer.clearLayers();

if (!latLngNames) return;

for (var i = 0; i < latLngNames.length; i++) {
var r = latLngNames[i];
this.addMarker(r.latlng, r.name);
}
},

getMarkers: function() {
return this.markersLayer.getLayers().map(function(it) {
return {
latlng: it._latlng,
name: it._popup._content
};
});
}
});
47 changes: 43 additions & 4 deletions js/router/BRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ L.BRouter = L.Class.extend({
L.setOptions(this, options);
},

getUrlParams: function(latLngs, format) {
getUrlParams: function(latLngs, pois, format) {
params = {};
if (this._getLonLatsString(latLngs) != null) params.lonlats = this._getLonLatsString(latLngs);

Expand All @@ -53,6 +53,8 @@ L.BRouter = L.Class.extend({

if (this.options.profile != null) params.profile = this.options.profile;

if (pois && this._getLonLatsNameString(pois) != null) params.poi = this._getLonLatsNameString(pois);

params.alternativeidx = this.options.alternative;

if (format != null) {
Expand Down Expand Up @@ -91,15 +93,19 @@ L.BRouter = L.Class.extend({
if (params.profile) {
opts.profile = params.profile;
}
if (params.poi) {
opts.poi = this._parseLonLatNames(params.poi);
}
return opts;
},

getUrl: function(latLngs, format, trackname, exportWaypoints) {
var urlParams = this.getUrlParams(latLngs, format);
getUrl: function(latLngs, pois, format, trackname, exportWaypoints) {
var urlParams = this.getUrlParams(latLngs, pois, format);

var args = [];
if (urlParams.lonlats != null && urlParams.lonlats.length > 0)
args.push(L.Util.template('lonlats={lonlats}', urlParams));
if (urlParams.poi != null && urlParams.poi.length > 0) args.push(L.Util.template('poi={poi}', urlParams));
if (urlParams.nogos != null) args.push(L.Util.template('nogos={nogos}', urlParams));
if (urlParams.polylines != null) args.push(L.Util.template('polylines={polylines}', urlParams));
if (urlParams.polygons != null) args.push(L.Util.template('polygons={polygons}', urlParams));
Expand All @@ -120,7 +126,7 @@ L.BRouter = L.Class.extend({
},

getRoute: function(latLngs, cb) {
var url = this.getUrl(latLngs, 'geojson'),
var url = this.getUrl(latLngs, null, 'geojson'),
xhr = new XMLHttpRequest();

if (!url) {
Expand Down Expand Up @@ -231,6 +237,39 @@ L.BRouter = L.Class.extend({
return lonlats;
},

_getLonLatsNameString: function(latLngNames) {
var s = '';
for (var i = 0; i < latLngNames.length; i++) {
s += this._formatLatLng(latLngNames[i].latlng);
s += L.BRouter.NUMBER_SEPARATOR;
s += latLngNames[i].name;

if (i < latLngNames.length - 1) {
s += L.BRouter.GROUP_SEPARATOR;
}
}
return s;
},

_parseLonLatNames: function(s) {
var groups,
part,
lonlatnames = [];

if (!s) {
return lonlatnames;
}

groups = s.split(L.BRouter.GROUP_SEPARATOR);
for (var i = 0; i < groups.length; i++) {
// lng,lat,name
part = groups[i].split(L.BRouter.NUMBER_SEPARATOR);
lonlatnames.push({ latlng: L.latLng(part[1], part[0]), name: decodeURI(part[2]) });
}

return lonlatnames;
},

_getNogosString: function(nogos) {
var s = '';
for (var i = 0, circle; i < nogos.length; i++) {
Expand Down

0 comments on commit 5ec4cbe

Please sign in to comment.