Search code examples
javascriptgoogle-mapsgoogle-maps-api-3google-maps-markers

Log marker position only once after it's finished dragging


Whenever I am dragging my marker, the getPosition is being console logged every 0.1s while the element is being dragged. How can I make it only console log when it's being stopped dragged? I want the same effect as .getRadius() works, it only console logs it when the radius is being changed once.

Unwanted effect, I want to log it only once after position change

  marker.Circle.addListener('center_changed', function() {
          console.log(marker.getPosition())
        })

Solution

  • Instead of using the center_changed event, use the dragend one.

    marker.Circle.addListener('dragend', function() {
       console.log(marker.getPosition())
    });
    

    https://developers.google.com/maps/documentation/javascript/reference/polygon#Circle.dragend

    Also it's actually a Circle, not a Marker, that's being dragged. Only Circle and Map objects have the center_changed event. So it looks like it's a circle attached to your marker.