A
A
AngryYum2019-09-02 22:35:35
JavaScript
AngryYum, 2019-09-02 22:35:35

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

2 answer(s)
0
0xD34F, 2019-09-03
@AngryYum

<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);
    }
  });
});

jsfiddle.net/7kh08L4m

S
sashabeep, 2019-09-02
@sashabeep

...
var addr = new Array(xx.xxxxxxx, yy.yyyyyyyy); 
myMap.panTo(addr, {duration: 4000});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question