Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google maps apiv3 'click' and 'dragend' single listener

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.

like image 584
Ryler Sexton Avatar asked Jan 13 '13 06:01

Ryler Sexton


People also ask

How do I add an event to Google Maps?

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.

How do I find the latitude and longitude of a marker on Google Maps?

getPosition() returns an object, so to specifically get latitude/longitude, you'll have to call lat() and lng() respectively from that.

How do I see events on Google Maps?

To ensure that your events appear on Google Maps, enter the address in the “Where” box when you create an event in Google Calendar. As long as you're signed into both Google Calendar and Google Maps, the next time you open Google Maps you'll see your Google Calendar events right there on the map.

Which is the preferred method for handling large number of markers is to store the data in?

As it turns out, the Android Maps Util Library https://github.com/googlemaps/android-maps-utils seems to be the number one choice when clustering markers.


1 Answers

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:

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});
like image 105
Dr.Molle Avatar answered Oct 31 '22 16:10

Dr.Molle