Skip to content

Commit

Permalink
Add markers POI layer
Browse files Browse the repository at this point in the history
  • Loading branch information
bagage committed Oct 9, 2019
1 parent ed53a58 commit 882c4da
Show file tree
Hide file tree
Showing 9 changed files with 244 additions and 20 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ nbproject/
/dist
brouter-web.*.zip
yarn-error.log
package-lock.json
3 changes: 3 additions & 0 deletions css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ input#trackname:focus:invalid {
.routing-draw-enabled {
cursor: crosshair;
}
.pois-draw-enabled {
cursor: cell;
}

#map {
/* center error message horizontally */
Expand Down
5 changes: 3 additions & 2 deletions js/control/Export.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
BR.Export = L.Class.extend({
latLngs: [],

initialize: function(router) {
initialize: function(router, pois) {
this.router = router;
this.pois = pois;
this.exportButton = $('#exportButton');
var trackname = (this.trackname = document.getElementById('trackname'));
this.tracknameAllowedChars = BR.conf.tracknameAllowedChars;
Expand Down Expand Up @@ -38,7 +39,7 @@ 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, this.pois.getMarkers(), 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
44 changes: 32 additions & 12 deletions js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
sidebar,
drawButton,
deleteRouteButton,
drawToolbar,
pois,
urlHash,
saveWarningShown = false;

Expand Down Expand Up @@ -93,7 +93,7 @@
function() {
bootbox.prompt({
size: 'small',
title: i18next.t('map.delete-route-nogos'),
title: i18next.t('map.clear-route'),
inputType: 'checkbox',
inputOptions: [
{
Expand All @@ -103,6 +103,10 @@
{
text: i18next.t('map.delete-nogo-areas'),
value: 'nogo'
},
{
text: i18next.t('map.delete-pois'),
value: 'pois'
}
],
value: ['route'],
Expand All @@ -114,13 +118,16 @@
if (result.indexOf('nogo') !== -1) {
nogos.clear();
}
if (result.indexOf('pois') !== -1) {
pois.clear();
}
onUpdate();
urlHash.onMapMove();
}
}
});
},
i18next.t('map.delete-route-nogos')
i18next.t('map.clear-route')
);

function updateRoute(evt) {
Expand Down Expand Up @@ -160,7 +167,6 @@
} else {
stats = new BR.TrackStats();
}
exportRoute = new BR.Export(router);
elevation = new BR.Elevation();

profile = new BR.Profile();
Expand Down Expand Up @@ -205,6 +211,12 @@
styles: BR.conf.routingStyles
});

pois = new BR.PoiMarkers({
routing: routing
});

exportRoute = new BR.Export(router, pois);

routing.on('routing:routeWaypointEnd routing:setWaypointsEnd', function(evt) {
search.clear();
onUpdate(evt && evt.err);
Expand Down Expand Up @@ -245,6 +257,8 @@
routing.addTo(map);
elevation.addBelow(map);

pois.addTo(map);

sidebar = BR.sidebar({
defaultTabId: BR.conf.transit ? 'tab_itinerary' : 'tab_profile',
listeningTabs: {
Expand All @@ -257,13 +271,7 @@
}

nogos.addTo(map);
L.easyBar([
drawButton,
reverseRouteButton,
nogos.getButton(),
deletePointButton,
deleteRouteButton
]).addTo(map);
L.easyBar([drawButton, reverseRouteButton, nogos.getButton(), deletePointButton, deleteRouteButton]).addTo(map);
nogos.preventRoutePointOnCreate(routing);

if (BR.keys.strava) {
Expand Down Expand Up @@ -304,6 +312,7 @@
return p;
};
if (url == null) return;

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

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

var onInvalidHashChangeCb = function(params) {
Expand All @@ -327,9 +340,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(), pois.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 All @@ -346,6 +363,7 @@

routingOptions.on('update', urlHash.onMapMove, urlHash);
nogos.on('update', urlHash.onMapMove, urlHash);
pois.on('update', urlHash.onMapMove, urlHash);
// waypoint add, move, delete (but last)
routing.on('routing:routeWaypointEnd', urlHash.onMapMove, urlHash);
// delete last waypoint
Expand Down Expand Up @@ -394,6 +412,8 @@
});
}

L.AwesomeMarkers.Icon.prototype.options.prefix = 'fa';

i18next
.use(window.i18nextXHRBackend)
.use(window.i18nextBrowserLanguageDetector)
Expand Down
144 changes: 144 additions & 0 deletions js/plugin/POIMarkers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
BR.PoiMarkers = L.Control.extend({
markersLayer: null,

options: {
routing: null,
shortcut: {
draw: {
enable: 80, // char code for 'p'
disable: 27 // char code for 'ESC'
}
}
},

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

this.map = map;
this.markersLayer = L.layerGroup([]).addTo(map);

this.drawButton = L.easyButton({
states: [
{
stateName: 'activate-poi',
icon: 'fa-hand-o-right',
onClick: function() {
self.draw(true);
},
title: i18next.t('map.draw-poi-start')
},
{
stateName: 'deactivate-poi',
icon: 'fa-hand-o-right active',
onClick: function() {
self.draw(false);
},
title: i18next.t('map.draw-poi-stop')
}
]
}).addTo(map);

map.on('routing:draw-start', function() {
self.draw(false);
});

var container = new L.DomUtil.create('div');
// keys not working when map container does not have focus, use document instead
L.DomEvent.removeListener(container, 'keyup', this._keyupListener);
L.DomEvent.addListener(document, 'keyup', this._keyupListener, this);

return container;
},

draw: function(enable) {
this.drawButton.state(enable ? 'deactivate-poi' : 'activate-poi');
if (enable) {
this.options.routing.draw(false);
this.map.on('click', this.onMapClick, this);
L.DomUtil.addClass(this.map.getContainer(), 'pois-draw-enabled');
} else {
this.map.off('click', this.onMapClick, this);
L.DomUtil.removeClass(this.map.getContainer(), 'pois-draw-enabled');
}
},

_keyupListener: function(e) {
// Suppress shortcut handling when a text input field is focussed
if (document.activeElement.type == 'text' || document.activeElement.type == 'textarea') {
return;
}
if (e.keyCode === this.options.shortcut.draw.disable) {
this.draw(false);
} else if (e.keyCode === this.options.shortcut.draw.enable) {
this.draw(true);
}
},

onMapClick: function(e) {
var self = this;
bootbox.prompt({
title: i18next.t('map.enter-poi-name'),
callback: function(result) {
if (result !== null) {
self.addMarker(e.latlng, result);
}
}
});
},

addMarker: function(latlng, name) {
var sanitizeHTML = function(str) {
var temp = document.createElement('div');
temp.textContent = str;
return temp.innerHTML;
};

var icon = L.AwesomeMarkers.icon({
icon: 'star',
markerColor: 'cadetblue'
});
var content = sanitizeHTML(name) + '<br>';
content += "<button id='remove-poi-marker' class='btn btn-secondary'><i class='fa fa-trash'></i></button>";

var self = this;
var marker = L.marker(latlng, { icon: icon, draggable: true, name: name })
.bindPopup(content)
.on('dragend', function() {
self.fire('update');
})
.on('popupopen', function() {
$('#remove-poi-marker').on('click', function(e) {
self.markersLayer.removeLayer(marker);
e.preventDefault();
self.fire('update');
});
})
.addTo(this.markersLayer);
},

clear: function() {
this.markersLayer.clearLayers();
},

setMarkers: function(latLngNames) {
this.clear();

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.options.name
};
});
}
});

BR.PoiMarkers.include(L.Evented.prototype);
Loading

0 comments on commit 882c4da

Please sign in to comment.