I am using google map v3 api. I need to detect the drag event on the map. Be it the drag on the map to move to a nearby geographical location or a drag on the marker. I need some javascript function to run when either of the events occur.
To add an event, head to Google Maps on Android, tap on Contribute >Events > Add a public event. You can add an event name, tag the location, and add the time and date of the event as well. There's an option to add an image, write more event details, and add description as well.
Long press the marker to enable dragging. When you take your finger off the screen, the marker will remain in that position. Markers are not draggable by default. You must explicitly set the marker to be draggable either with MarkerOptions.
As opposed to statically setting markers (pins) programmatically sometimes you might want your users to be able to interact with the map by dragging existing markers around the map. Fortunately, Google’s JavaScript API offers this functionality out of the box. Below you’ll find an example based on Google Maps API V3.
Draggable Pin Marker for Google Maps API V3. Google’s Maps API offer a vast range of functionality to interact with underlying maps. As opposed to statically setting markers (pins) programmatically sometimes you might want your users to be able to interact with the map by dragging existing markers around the map.
Some objects within the Maps JavaScript API are designed to respond to user events such as mouse or keyboard events. For example, these are some of the user events that a google.maps.Marker object can listen to: 'click'. 'dblclick'. 'mouseup'. 'mousedown'. 'mouseover'. 'mouseout'.
Google’s Maps API offer a vast range of functionality to interact with underlying maps. As opposed to statically setting markers (pins) programmatically sometimes you might want your users to be able to interact with the map by dragging existing markers around the map.
Both Map objects and Marker objects have drag
events, although you probably want dragend
so that you can do something when a user is done dragging rather than do something while a user is dragging.
So you could do something like this:
google.maps.event.addListener(map, 'dragend', function() { alert('map dragged'); } ); google.maps.event.addListener(marker, 'dragend', function() { alert('marker dragged'); } );
google.maps.event.addListener(map, "mouseover", function (e) { google.maps.event.addListener(map, "dragend", function () { setTimeout(() => { console.log(e); // this contains the information about the coordinates }); }); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With