Q
Q
qbk172017-08-16 10:18:55
Yandex maps
qbk17, 2017-08-16 10:18:55

How to change Ymap center when changing resolution?

There is a map with labels, when I change the resolution, the labels slide out of sight. How to change the center of the map at a certain resolution?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Igor, 2019-02-24
@peretc001

ymaps.ready(init);
    //Set default center and zoom
    function init() {
        myMap = new ymaps.Map('map_page', {
            center: [45.0701, 38.9048],
            zoom: 9
        });


        //Set user Icon
        s = {
            iconLayout: 'default#image',
            iconImageHref: '/wp-content/themes/okhall/img/logo-map.svg',
            iconImageSize: [87, 74],
            iconImageOffset: [-43, -56]
        };
        //Set some points
        m = {
            m1: new ymaps.Placemark([45.0710, 38.9038], {}, s),
            m1center: [45.0701, 38.9048],
            m2: new ymaps.Placemark([45.0349, 39.1317], {}, s),
            m2center: [45.0349, 39.1317],
            m3: new ymaps.Placemark([44.7491, 37.7263], {}, s),
            m3center: [44.7491, 37.7265],
            m4: new ymaps.Placemark([45.2299, 38.1138], {}, s),
            m4center: [45.2299, 38.1138],
            m5: new ymaps.Placemark([45.8417, 40.1357], {}, s),
            m5center: [45.8417, 40.1357],
            m6: new ymaps.Placemark([44.9367, 37.9675], {}, s),
            m6center: [44.9367, 37.9675]
        };
        //Disabled scroll
        //myMap.behaviors.disable('scrollZoom');
        //on mobile disable touch
        if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
            //... отключаем перетаскивание карты
            myMap.behaviors.disable('drag');
        } 
       //Add points on the map
        myMap.geoObjects
            .add(m['m1'])
            .add(m['m2'])
            .add(m['m3'])
            .add(m['m4'])
            .add(m['m5'])
            .add(m['m6']);

        // ON RESIZE
        //Get curent center and zoom
        var pixelCenter = myMap.getGlobalPixelCenter('map_page');
        console.log(pixelCenter);

        //
        function onResizeMap() {
        if ($(window).width() > '992') { 
            //Set New center
            myMap.setCenter([45.0701, 37.0048]);
            var pixelCenter2 = myMap.getGlobalPixelCenter('map_page');
            console.log(pixelCenter2);
            } else {
                myMap.setCenter([45.0701, 38.9048]);
            }
        } onResizeMap();

        window.onresize = function () {
            onResizeMap();
        };
    };

F
freeExec, 2017-08-16
@freeExec

map.setCenter(lonlat);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question