Search code examples
javascriptyandex-maps

how to get the current map coordinates?


I have yandex map on page. For this map i bind event handler 'actionend'.

I need in this handler get current coordinates of map center. Please help me make it.

live demo here.

Code:

ymaps.ready(init);

var myMap;

function init(){     
    myMap = new ymaps.Map("map", {
        center: [55.76, 37.64],
        controls: [],
        zoom: 18
    });   

    myMap.events.add('actionend', function (e) {
        console.log('stop action');
        console.log('print current coords of map center???????????????');
    });
};

Solution

  • You can get the center coord by calling the getCenter() method, like this:

    ymaps.ready(init);
    
    var myMap;
    
    function init(){     
        myMap = new ymaps.Map("map", {
            center: [55.76, 37.64],
            controls: [],
            zoom: 18
        });   
    
        myMap.events.add('actionend', function (e) {
           console.log(e.originalEvent.map.getCenter())
        });
    };
    

    See this doc.