Z
Z
z_u_q2019-02-01 00:33:45
JavaScript
z_u_q, 2019-02-01 00:33:45

How to compare the coordinates that come from two arrays with each other?

Hello. There are links - "Label 1 Address" and "Label 2 Address", when clicked, the map is centered relative to the label 1 or 2. I want to make it so that when you click on "Label Address 1" or "Label Address 2", the corresponding description of the label opens .

In fact, it turns out that address-position gives me an array with all coordinates, and an array with all coordinates also comes from map-balloonContent. Here I want to compare the elements of this array. And if when you click on the link (for example, "Label address 1") coincides with the coordinate of the label, then open this description of the label

. I tried to do this, but it didn't work out.

elem.on('clickAddress', function(e, coord) {
  var result = [];
  coordinates.forEach( function (element) {
      if ( !~coord.indexOf(element) ) {
      	result.push(element); 
      	console.log(result);
      	placemark.balloon.open();
      }
  });
});


Link to example

Answer the question

In order to leave comments, you need to log in

2 answer(s)
0
0xD34F, 2019-02-01
@z_u_q

You overcomplicated something, everything is much simpler:

if (coord.every((n, i) => n === coordinates[i])) {
  placemark.balloon.open();
}

But in general, it is necessary to rewrite - you have a clickAddress event handler assigned several times, although one is enough (and in general - verbose somehow). For example, like this:
const placemarks = elem.find('.map-balloonContent').get().map(n => createPlacemark({
  coord: $(n).data('coord'),
  content: $(n).html(),
}));
placemarks.forEach(n => myMap.geoObjects.add(n));
placemarks[0].balloon.open();

elem.on('clickAddress', function(e, coord) {
  placemarks.find(n => n.geometry.getCoordinates().every((m, i) => m === coord[i])).balloon.open();
});

P
Pavlo Ponomarenko, 2019-02-01
@TheShock

Abandon ForIch, divide the method into two logical parts - search and action. I'm not sure what you have and how it works (maybe it's impossible to compare using indexOf, although you do compare)

function matches (point, target) {
  return point === target; // тут надо правильное условие написать
}

function findIn (coordinates, target) {
  for (var i = 0; i < coordinates.length; i++) {
    if (matches(coordinates[i], target)) return coordinates[i];
  }

  return null;
}

elem.on('clickAddress', function(e, coord) {
  const result = findIn( coordinates, coord );
  if (result) {
      	placemark.balloon.open();
  }
});

I know that your logic is not quite the same, but I'm trying to convey the idea. If you can open several balloons at the same time, return an array. While you have written something incomprehensible. There is especially a lack of typing, for example, what is in the coord variable - it is completely incomprehensible that you are calling it indexOf.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question