Answer the question
In order to leave comments, you need to log in
How to change the center on the Yandex map on click?
There is a Yandex map with 3 labels (addresses). Also brought these 3 addresses to another block. How can I organize that by clicking on the addresses in the block, they will jump to the label on the Yandex map?
Answer the question
In order to leave comments, you need to log in
<div id="buttons"></div>
<div id="map"></div>
const items = [
{ coord: [ 55.751244, 37.618423 ], title: 'Moscow' },
{ coord: [ 48.864716, 2.349014 ], title: 'Paris' },
{ coord: [ 34.052235, -118.243683 ], title: 'Los Angeles' },
];
ymaps.ready(function () {
const map = new ymaps.Map('map', {
zoom: 9,
center: items[Math.random() * items.length | 0].coord,
});
items.forEach(n => map.geoObjects.add(new ymaps.Placemark(n.coord)));
const buttons = document.querySelector('#buttons');
buttons.innerHTML = items.map((n, i) =>
`<button data-index="${i}">${n.title}</button>`
).join('');
buttons.addEventListener('click', ({ target: { dataset: { index } } }) => {
if (index) {
map.setCenter(items[index].coord);
}
});
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question