Render polygons from URL hash and pass it to BRouter server

This commit is contained in:
Phyks (Lucas Verney) 2019-02-18 21:00:36 +01:00
parent 70a0f73d12
commit 6e60751db0
2 changed files with 52 additions and 18 deletions

View file

@ -17,7 +17,7 @@ BR.NogoAreas = L.Control.extend({
fillOpacity: 0.2, fillOpacity: 0.2,
dashArray: null dashArray: null
}, },
editStyle: { editStyle: {
color: '#fe57a1', color: '#fe57a1',
opacity: 0.6, opacity: 0.6,
@ -31,7 +31,7 @@ BR.NogoAreas = L.Control.extend({
onAdd: function (map) { onAdd: function (map) {
var self = this; var self = this;
this.drawnItems = new L.FeatureGroup().addTo(map); this.drawnItems = new L.FeatureGroup().addTo(map);
this.drawnItems.on('click', function(e) { this.drawnItems.on('click', function(e) {
L.DomEvent.stop(e); L.DomEvent.stop(e);
@ -109,7 +109,7 @@ BR.NogoAreas = L.Control.extend({
routing.draw(true); routing.draw(true);
}, 0); }, 0);
} }
}, this); }, this);
}, },
getOptions: function() { getOptions: function() {
@ -120,6 +120,8 @@ BR.NogoAreas = L.Control.extend({
setOptions: function(options) { setOptions: function(options) {
var nogos = options.nogos; var nogos = options.nogos;
var polylines = options.polylines;
var polygons = options.polygons;
this._clear(); this._clear();
if (nogos) { if (nogos) {
for (var i = 0; i < nogos.length; i++) { for (var i = 0; i < nogos.length; i++) {
@ -127,6 +129,18 @@ BR.NogoAreas = L.Control.extend({
this.drawnItems.addLayer(nogos[i]); this.drawnItems.addLayer(nogos[i]);
} }
} }
if (polylines) {
for (var i = 0; i < polylines.length; i++) {
polylines[i].setStyle(this.style);
this.drawnItems.addLayer(polylines[i]);
}
}
if (polygons) {
for (var i = 0; i < polygons.length; i++) {
polygons[i].setStyle(this.style);
this.drawnItems.addLayer(polygons[i]);
}
}
}, },
_clear: function () { _clear: function () {
@ -141,15 +155,15 @@ BR.NogoAreas = L.Control.extend({
_fireUpdate: function () { _fireUpdate: function () {
this.fire('update', {options: this.getOptions()}); this.fire('update', {options: this.getOptions()});
}, },
getFeatureGroup: function() { getFeatureGroup: function() {
return this.drawnItems; return this.drawnItems;
}, },
getEditGroup: function() { getEditGroup: function() {
return this.editTools.editLayer; return this.editTools.editLayer;
}, },
getButton: function() { getButton: function() {
return this.button; return this.button;
} }
@ -175,7 +189,7 @@ BR.EditingTooltip = L.Handler.extend({
}, },
addHooks: function () { addHooks: function () {
// hack: listen to EasyButton click (instead of editable:drawing:start), // hack: listen to EasyButton click (instead of editable:drawing:start),
// to get mouse position from event for initial tooltip location // to get mouse position from event for initial tooltip location
L.DomEvent.addListener(this.button.button, 'click', this._addCreate, this); L.DomEvent.addListener(this.button.button, 'click', this._addCreate, this);
@ -197,7 +211,7 @@ BR.EditingTooltip = L.Handler.extend({
}, },
_bind: function (e) { _bind: function (e) {
// Position tooltip at bottom of circle, less distracting than // Position tooltip at bottom of circle, less distracting than
// sticky with cursor or at center. // sticky with cursor or at center.
var layer = e.layer; var layer = e.layer;
@ -206,7 +220,7 @@ BR.EditingTooltip = L.Handler.extend({
className: 'editing-tooltip' className: 'editing-tooltip'
}); });
// Override to set position to south instead of center (circle latlng); // Override to set position to south instead of center (circle latlng);
// works better with zooming than updating offset to match radius // works better with zooming than updating offset to match radius
layer.openTooltip = function (layer, latlng) { layer.openTooltip = function (layer, latlng) {
if (!latlng && layer instanceof L.Layer) { if (!latlng && layer instanceof L.Layer) {
@ -219,7 +233,7 @@ BR.EditingTooltip = L.Handler.extend({
_addCreate: function (e) { _addCreate: function (e) {
// button cancel // button cancel
if (!this.editTools.drawing()) return; if (!this.editTools.drawing()) return;
var initialLatLng = this.map.mouseEventToLatLng(e); var initialLatLng = this.map.mouseEventToLatLng(e);
var tooltip = L.tooltip({ var tooltip = L.tooltip({
// no effect with map tooltip // no effect with map tooltip
@ -256,7 +270,7 @@ BR.EditingTooltip = L.Handler.extend({
this.map.closeTooltip(tooltip); this.map.closeTooltip(tooltip);
}; };
this.editTools.once('editable:editing editable:drawing:cancel', closeTooltip, this); this.editTools.once('editable:editing editable:drawing:cancel', closeTooltip, this);
if (BR.Browser.touch) { if (BR.Browser.touch) {
// can't move with cursor on touch devices, so show at start pos for a few seconds // can't move with cursor on touch devices, so show at start pos for a few seconds
setTimeout(L.bind(closeTooltip, this), this.options.closeTimeout); setTimeout(L.bind(closeTooltip, this), this.options.closeTimeout);
@ -267,7 +281,7 @@ BR.EditingTooltip = L.Handler.extend({
var timeoutId = setTimeout(function () { var timeoutId = setTimeout(function () {
layer.closeTooltip(); layer.closeTooltip();
}, this.options.closeTimeout); }, this.options.closeTimeout);
// prevent timer to close tooltip that changed in the meantime // prevent timer to close tooltip that changed in the meantime
layer.once('tooltipopen', function (e) { layer.once('tooltipopen', function (e) {
clearTimeout(timeoutId); clearTimeout(timeoutId);
@ -286,7 +300,7 @@ BR.EditingTooltip = L.Handler.extend({
_enable: function (e) { _enable: function (e) {
e.layer.setTooltipContent(BR.NogoAreas.MSG_ENABLED); e.layer.setTooltipContent(BR.NogoAreas.MSG_ENABLED);
this.editTools.once('editable:editing', function(e) { this.editTools.once('editable:editing', function(e) {
e.layer.closeTooltip(); e.layer.closeTooltip();
}, this); }, this);
@ -353,7 +367,7 @@ BR.DeletableCircleEditor = L.Editable.CircleEditor.extend({
onDrawingMouseDown: function (e) { onDrawingMouseDown: function (e) {
this._deleteLatLng.update(e.latlng); this._deleteLatLng.update(e.latlng);
L.Editable.CircleEditor.prototype.onDrawingMouseDown.call(this, e); L.Editable.CircleEditor.prototype.onDrawingMouseDown.call(this, e);
}, },
// override to cancel/remove created circle when added by click instead of drag, because: // override to cancel/remove created circle when added by click instead of drag, because:
// - without resize, edit handles stacked on top of each other // - without resize, edit handles stacked on top of each other

View file

@ -282,6 +282,10 @@ L.BRouter = L.Class.extend({
} }
s += this._formatLatLng(vertices[j]); s += this._formatLatLng(vertices[j]);
} }
if (polyline.options.nogoWeight) {
s += L.BRouter.NUMBER_SEPARATOR;
s += polyline.options.nogoWeight;
}
if (i < (nogos.length - 1)) { if (i < (nogos.length - 1)) {
s += L.BRouter.GROUP_SEPARATOR; s += L.BRouter.GROUP_SEPARATOR;
} }
@ -304,9 +308,15 @@ L.BRouter = L.Class.extend({
latlngs = []; latlngs = [];
for (var j = 0; j < numbers.length - 1;) for (var j = 0; j < numbers.length - 1;)
{ {
latlngs.push([numbers[j++], numbers[j++]]); var lng = Number.parseFloat(numbers[j++]);
var lat = Number.parseFloat(numbers[j++]);
latlngs.push([lat, lng]);
} }
nogos.push(L.polyline(latlngs)); var nogoWeight;
if (j < numbers.length) {
nogoWeight = Number.parseFloat(numbers[j++]);
}
nogos.push(L.polyline(latlngs, { nogoWeight: nogoWeight }));
} }
} }
return nogos; return nogos;
@ -323,6 +333,10 @@ L.BRouter = L.Class.extend({
} }
s += this._formatLatLng(vertices[j]); s += this._formatLatLng(vertices[j]);
} }
if (polygon.options.nogoWeight) {
s += L.BRouter.NUMBER_SEPARATOR;
s += polygon.options.nogoWeight;
}
if (i < (nogos.length - 1)) { if (i < (nogos.length - 1)) {
s += L.BRouter.GROUP_SEPARATOR; s += L.BRouter.GROUP_SEPARATOR;
} }
@ -345,9 +359,15 @@ L.BRouter = L.Class.extend({
latlngs = []; latlngs = [];
for (var j = 0; j < numbers.length - 1;) for (var j = 0; j < numbers.length - 1;)
{ {
latlngs.push([numbers[j++], numbers[j++]]); var lng = Number.parseFloat(numbers[j++]);
var lat = Number.parseFloat(numbers[j++]);
latlngs.push([lat, lng]);
} }
nogos.push(L.polygon(latlngs)); var nogoWeight;
if (j < numbers.length) {
nogoWeight = Number.parseFloat(numbers[j++]);
}
nogos.push(L.polygon(latlngs, { nogoWeight: nogoWeight }));
} }
} }
return nogos; return nogos;