Можно ли смещать карту на определенное расстояние при клике по маркеру?
Подскажите, пожалуйста. Есть карта с маркерами. Маркеры могут находится близко к краю карты. Как при клике на маркер проверять, если он к краю ближе чем 100px*100px справа, то двигать карту (не центровать) на это расстояние.
let marker = new Ymaps.Placemark([lat, lng], {}, {
hideIconOnBalloonOpen: false,
iconLayout: this.createMarkerLayout(group, '36px'),
iconOffset: [-18, -18],
iconShape: { type: 'Circle', coordinates: [18, 18], radius: 18 },
iconColor: group[0].targeted ? '#FF6458' : '#1BA9F9',
balloonLayout: this.createPopupLayout(group[0]),
balloonOffset: [12, -22],
})
marker.events.add('click', e => {
// думаю, где-то здесь надо что-то делать, так он центрует карту относительно метки
this.map.panTo([lat, lng], {flying: false, safe: false})
})
this.markers.push(marker)
this.map.geoObjects.add(new Ymaps.GeoObjectCollection({ children: this.markers }))
createPopupLayout (board) {
return Ymaps.templateLayoutFactory.createClass(`
<div class="custom-popup">....</div> `,
{
build: function () {
this.constructor.superclass.build.call(this)
this._$element = this.getParentElement().querySelector('.custom-popup')
this._$element.style.top = `-${(this._$element.offsetHeight)}px`
this._$element.addEventListener('click', this.click.bind(this))
},
clear: function () {
this._$element.removeEventListener('click', this.click)
this.constructor.superclass.clear.call(this)
},
click: async function ({target}) {
const root = this.getParentElement().querySelector('.custom-popup')
},
})}
Ответы (1 шт):
Автор решения: YaSupport
→ Ссылка
Чтобы кастомные балуны учитывались опцией autoPan, им следует правильно задавать форму через getShape. Рабочий пример есть в нашей песочнице.