Answer the question
In order to leave comments, you need to log in
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();
}
});
});
Answer the question
In order to leave comments, you need to log in
You overcomplicated something, everything is much simpler:
if (coord.every((n, i) => n === coordinates[i])) {
placemark.balloon.open();
}
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();
});
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();
}
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question