working on my code to retrieve lat,long by clicking and it works fine. wondering if its possible to combine ‘click’ and ‘dragend’ in a single event listener if not. Would appreciate whats a proper alternative.
This is what I’ve been working on.
google.maps.event.addListener(map,'click', function(event) {
document.getElementById("latbox").value = event.latLng.lat();
document.getElementById("lngbox").value = event.latLng.lng();
addMarker(event.latLng);
});
}
function addMarker(location) {
if (!marker) {
marker = new google.maps.Marker({
position: location,
map: map,
draggable:true,
animation: google.maps.Animation.DROP
});
}
// Otherwise, simply update its location on the map.
else { marker.setPosition(location); }
}
I tried doing this google.maps.event.addListener(map,'click','dragend', function(event) but to no avail. also if anyone has an idea to modify this marker.setPosition(location); to have animation: google.maps.Animation.DROP . . thanks in advance.
There is no built-in method to apply a listener for multiple events.
When your problem is that you don’t want to define the callback-function twice, you may either use a non-anonymous function or define the listener for 1 event and trigger the event when the other event fires:
Related to the animation:
Instead of using the marker-methods(setAnimation,setPosition) to set marker-options, you also may use the method setOptions to set multiple options at once: