@black_xe

Kак изменить центр существующей карты Ya Maps API?

У меня есть два метода:
1) mainMap() - создание самой карты.
2) setPoint() - изменяет ее центр при нажатии на кнопку.
Координаты я все определил, все данные есть.
ПРОБЛЕМА в том, что у меня не получается в методе setPoint() изменить созданную карту в mainMap().

let deliveryMap = {
    run() {
        deliveryMap.mainMap();
        $(document).on('click', '[data-store-id]', event => this.setPoint(event));
    },

    setPoint(event) {
        let storeId = event.currentTarget.attributes['data-store-id'].value;
        let storesPoints = [stores[storeId]['GPS_N'], stores[storeId]['GPS_S']];
        /*ymaps.ready(function () {
            let myMap = new ymaps.Map('mapStores', {
                center: storesPoints,
                zoom: 13,
                controls: []
            }, {
                searchControlProvider: 'yandex#search'
            });
            myMap.setCenter([51.781876, 55.095035], 10, {
                checkZoomRange: true
            });
        });*/
    },

    mainMap() {
        if ($('[data-stores-map]').length) {
            let tempValue = Object.values(stores);
            let storesPoints = [tempValue[0]['GPS_N'], tempValue[0]['GPS_S']];
            ymaps.ready(function () {
                let myMap = new ymaps.Map('mapStores', {
                    center: storesPoints,
                    zoom: 13,
                    controls: []
                }, {
                    searchControlProvider: 'yandex#search'
                });
                for (let j = 0; j < tempValue.length; j++) {
                    let storePoint = [tempValue[j]['GPS_N'], tempValue[j]['GPS_S']];
                    let specialText = '';
                    if (tempValue[j]['SCHEDULE'].length) {
                        specialText += '<b>Часы работы:</b><br>';
                        specialText += tempValue[j]['SCHEDULE'] + '</br>';
                    }
                    window['placemark_' + tempValue[j]['ID']] = new ymaps.Placemark(storePoint, {
                        balloonContentHeader: tempValue[j]['TITLE'],
                        balloonContentBody: specialText,
                        balloonContentFooter: '',
                        hintContent: tempValue[j]['TITLE']
                    });
                    myMap.geoObjects.add(window['placemark_' + tempValue[j]['ID']]);

                    if (tempValue.length > 1) {
                        myMap.setBounds(myMap.geoObjects.getBounds());
                        // myMap.setZoom(myMap.getZoom() - 1);
                    }
                }
            });
        }
    }
};

module.exports = deliveryMap;
  • Вопрос задан
  • 188 просмотров
Пригласить эксперта
Ответы на вопрос 1
@black_xe Автор вопроса
В итоге у меня не получилось реализовать лучше, чем сделать вот так:

let deliveryMap = {
    run() {
        deliveryMap.mainMap();
    },

    mainMap() {
        if ($('[data-stores-map]').length) {
            let tempValue = Object.values(stores);
            let storesPoints = [tempValue[0]['GPS_N'], tempValue[0]['GPS_S']];
            ymaps.ready(function () {
                let myMap = new ymaps.Map('mapStores', {
                    center: storesPoints,
                    zoom: 13,
                    controls: []
                }, {
                    searchControlProvider: 'yandex#search'
                });
                for (let j = 0; j < tempValue.length; j++) {
                    let storePoint = [tempValue[j]['GPS_N'], tempValue[j]['GPS_S']];
                    let specialText = '';
                    if (tempValue[j]['SCHEDULE'].length) {
                        specialText += '<b>Часы работы:</b><br>';
                        specialText += tempValue[j]['SCHEDULE'] + '</br>';
                    }
                    window['placemark_' + tempValue[j]['ID']] = new ymaps.Placemark(storePoint, {
                        balloonContentHeader: tempValue[j]['TITLE'],
                        balloonContentBody: specialText,
                        balloonContentFooter: '',
                        hintContent: tempValue[j]['TITLE']
                    });
                    myMap.geoObjects.add(window['placemark_' + tempValue[j]['ID']]);

                    if (tempValue.length > 1) {
                        myMap.setBounds(myMap.geoObjects.getBounds());
                    }
                }

                $(document).on('click', '[data-store-id]', function (e) {
                    e.preventDefault();
                    let storeId = e.currentTarget.attributes['data-store-id'].value;
                    let storesPoints = [stores[storeId]['GPS_N'], stores[storeId]['GPS_S']];
                    myMap.setCenter(storesPoints, 10, {
                        checkZoomRange: true
                    });
                });

            });
        }
    }
};

module.exports = deliveryMap;


Если у кого то есть идеи как более эстетичнее написать код, буду рад увидеть и выслушать советы и критику !)
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы