working on my code to retrieve lat,long by clicking and it works fine. wondering if its possible to bine '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.
working on my code to retrieve lat,long by clicking and it works fine. wondering if its possible to bine '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.
1 Answer
Reset to default 9There 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:
google.maps.event.addListener( map, 'click',
function(e){ alert('clicked or dragged');} );
google.maps.event.addListener( map, 'dragend', function(){
google.maps.event.trigger(this, 'click');} );
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:
marker.setOptions({ position : location,
animation : google.maps.Animation.DROP});