N
N
novikov__al2022-01-21 15:51:14
Yandex maps
novikov__al, 2022-01-21 15:51:14

Tag: getting coordinates and address when dragging or clicking (Yandex API)?

Hello. Actually, straight to the point ... There is a code

ymaps.ready(init);        
function init() {
  var myMap = new ymaps.Map("map", {
    center: [55.76, 37.64],
    zoom: 10
  }, {
    searchControlProvider: 'yandex#search'
  });
 
  /* Начальный адрес метки */
  var address = 'Россия, Москва, Тверская, д. 7';
 
  ymaps.geocode(address).then(function(res) {
    var coord = res.geoObjects.get(0).geometry.getCoordinates();
 
    var myPlacemark = new ymaps.Placemark(coord, null, {
      preset: 'islands#blueDotIcon',
      draggable: true
    });
 
    /* Событие dragend - получение нового адреса */
    myPlacemark.events.add('dragend', function(e){
      var cord = e.get('target').geometry.getCoordinates();
      $('#ypoint').val(cord);
      ymaps.geocode(cord).then(function(res) {
        var data = res.geoObjects.get(0).properties.getAll();
        $('#address').val(data.text);
      });
    });
    
    myMap.geoObjects.add(myPlacemark);	
    myMap.setCenter(coord, 15);
  });
}

source: https://snipp.ru/jquery/yamaps-drag-drop

Everything works great. My task is to move the label not only by dragging, but also moving by clicking.
Those. when the page is loaded, the label is in the specified place, optionally it is either dragged or moved to the clicked place by clicking, passing the coordinates and address in the same way.

What should be added to this code? I’ll say in advance that I don’t understand js at all ... Poke, please, with your nose!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Tkachev, 2022-01-27
@Real_Farmer

Track all clicks on the map
map.events.add('click', function (e) {
get click coordinates
var coords = e.get('coords');
move marker to new coordinates
myPlacemark ygeometry.setCoordinates(coords );
}) ;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question