D
D
Dmitry Samutin2018-09-10 15:36:38
JavaScript
Dmitry Samutin, 2018-09-10 15:36:38

How to find out the coordinates of which point on the multiroute have changed (yandex maps)?

Good day. Interested in the question of how to find out what changes have occurred on the route when editing it.
I found an event when changing the route, but I can’t find exactly what changes (with which points) occurred.
How can I find these changes?
So far I've implemented it like this:

class Ymap {
    constructor(mapId) {
        this.points = undefined;
        this.map = undefined;
        ymaps.ready(() => {
            this.map = new ymaps.Map(mapId, { center: [58, 40], zoom: 7, controls: []});
        });
        this.AddMultiRoute([]);
    }

     AddMultiRoute(geoPoints) {
        ymaps.ready(() => {
            this.multiRoute = new ymaps.multiRouter.MultiRoute({
                referencePoints: geoPoints
            }, {
                // Тип промежуточных точек, которые могут быть добавлены при редактировании.
                editorMidPointsType: "via",
                // В режиме добавления новых путевых точек запрещаем ставить точки поверх объектов карты.
                editorDrawOver: false
            });
            this.map.geoObjects.add(this.multiRoute);
        });
    }

    MultiRouteUpdate(func) {
        ymaps.ready(() => {
            this.multiRoute.events.add('update',
                function (e) {
                    Func(e.get('target').model.getReferencePoints(), this.points);
                    this.points = e.get('target').model.getReferencePoints();
                });
        });
    }
}

Where is the func function to compare arrays and find the difference, is there another way?
upd: One of 3 actions can happen at the same time:
1. one point is removed
2. one point is added
3. one point is changed
actually, you need to find this point and find out what exactly happened to it

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2018-09-10
@samutin

this.points = e.get('target').model.getReferencePoints();

Have you thought about the fact that when editing a route, the array that the map operates on can remain the same? - simply elements are added, removed, changed. If so (I have empirically established that this is the case for editing and deleting points), then at the time of comparison in this.points you will have the current version of the route, and not the previous one at all. And you will not be able to detect any changes - you will compare the array with itself. Make a copy:
As for the comparison function itself, everything looks somehow complicated for you - four cycles, why? It can be simpler - look in the old array for a point that is not in the new one, look for a point in the new array that is not in the old one. There are both points - it means a change has been made, there is only the old one - deletion, there is only a new one - addition. I would rewrite your function as something like this:
function comparison(newPoints, oldPoints) {
  const
    oldPoint = oldPoints.find(n => !newPoints.includes(n)),
    newPoint = newPoints.find(n => !oldPoints.includes(n));

  if (oldPoint && newPoint) {
    console.log('update', oldPoint, 'to', newPoint);
  } else if (oldPoint) {
    console.log('delete', oldPoint);
  } else if (newPoint) {
    console.log('create', newPoint);
  }
}

UPD. https://codepen.io/anon/pen/EzqVEN?editors=1010

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question