From 245b559e2d08033472b5244743b55e3fbe904c75 Mon Sep 17 00:00:00 2001 From: Fedor Katurov Date: Thu, 16 Aug 2018 15:39:54 +0700 Subject: [PATCH] router now properly handles poly changes --- src/modules/Editor.js | 10 ++++++++-- src/modules/Poly.js | 6 +++++- src/modules/Router.js | 28 +++++++++++++++++++++++++++- 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/src/modules/Editor.js b/src/modules/Editor.js index 714d96b..05175b6 100644 --- a/src/modules/Editor.js +++ b/src/modules/Editor.js @@ -12,9 +12,9 @@ export class Editor { }) { this.map = new Map({ container }); - const { lockMapClicks, map: { map } } = this; + const { lockMapClicks, routerMoveStart, map: { map } } = this; - this.poly = new Poly({ map: this.map.map }); + this.poly = new Poly({ map, routerMoveStart }); this.stickers = new Stickers({ map, lockMapClicks }); this.router = new Router({ map, lockMapClicks }); @@ -88,4 +88,10 @@ export class Editor { this.router.startFrom(latlngs.pop()); }; + + routerMoveStart = () => { + const { _latlngs } = this.poly.poly; + + if (_latlngs) this.router.moveStart(_latlngs[_latlngs.length-1]); + } } diff --git a/src/modules/Poly.js b/src/modules/Poly.js index aae234c..6cae492 100644 --- a/src/modules/Poly.js +++ b/src/modules/Poly.js @@ -3,24 +3,28 @@ import L from "leaflet"; const polyStyle = { color: '#ff3333', weight: '5' }; export class Poly { - constructor({ map }) { + constructor({ map, routerMoveStart }) { this.poly = L.polyline([], polyStyle); this.latlngs = []; this.poly.addTo(map); this.map = map; + this.routerMoveStart = routerMoveStart; this.bindEvents(); } updateMarks = () => { const coords = this.poly.toGeoJSON().geometry.coordinates; this.latlngs = (coords && coords.length && coords.map(([lng, lat]) => ({ lng, lat }))) || []; + + this.routerMoveStart(); }; bindEvents = () => { // Если на карте что-то меняется, пересчитать километражи this.map.editTools.addEventListener('editable:drawing:mouseup', this.updateMarks); this.map.editTools.addEventListener('editable:vertex:dragend', this.updateMarks); + this.map.editTools.addEventListener('editable:vertex:mouseup', this.updateMarks); this.map.editTools.addEventListener('editable:vertex:deleted', this.updateMarks); this.map.editTools.addEventListener('editable:vertex:new', this.updateMarks); diff --git a/src/modules/Router.js b/src/modules/Router.js index 9a6d4a7..fcf307a 100644 --- a/src/modules/Router.js +++ b/src/modules/Router.js @@ -95,6 +95,32 @@ export class Router { }; startFrom = latlngs => { + const waypoints = this.router.getWaypoints(); + + if (waypoints && waypoints.length) { + waypoints[0] = { ...latlngs }; + this.router.setWaypoints(waypoints); + return; + } + this.router.setWaypoints([{ ...latlngs }]); - } + }; + + moveStart = latlng => { + const waypoints = this.router.getWaypoints(); + const { latLng } = (waypoints[0] || {}); + + if (!latLng || !latlng) return; + + if ( + latlng.lat.toFixed(5) === latLng.lat.toFixed(5) && + latlng.lng.toFixed(5) === latLng.lng.toFixed(5) + ) { + return; + } + + waypoints[0] = { ...latlng }; + + this.router.setWaypoints(waypoints); + }; }