kim singh
kim singh

Reputation: 87

Google.maps.event.addListener for click and dragend doesn't work?

I have this code: the code in function() of 'click dragend' event doesn't execute and the latFld and llgFld in the document doesn't update, what do i do ?

DEMO && FULL CODE: http://jsfiddle.net/DSc7r/

...
     google.maps.event.addListener(map, 'click dragend', function (event) {
         $('.MapLat').val(event.latLng.lat());
         $('.MapLon').val(event.latLng.lng());
         alert(event.latLng.place.name)
     });
     $("#searchTextField").focusin(function () {
         $(document).keypress(function (e) {
             if (e.which == 13) {
                 return false;
                 infowindow.close();
                 var firstResult = $(".pac-container .pac-item:first").text();
                 var geocoder = new google.maps.Geocoder();
                 geocoder.geocode({
                     "address": firstResult
                 }, function (results, status) {
                     if (status == google.maps.GeocoderStatus.OK) {
                         var lat = results[0].geometry.location.lat(),
                             lng = results[0].geometry.location.lng(),
                             placeName = results[0].address_components[0].long_name,
                             latlng = new google.maps.LatLng(lat, lng);

                         moveMarker(placeName, latlng);
                         $("input").val(firstResult);
                         alert(firstResult)
                     }
                 });
             }
         });
     });

    ...

Upvotes: 2

Views: 4299

Answers (1)

Rob Schmuecker
Rob Schmuecker

Reputation: 8954

First off in the example code in the fiddle, autocomplete is not defined and hence is causing an error. As far as I know you cannot bind to more than 1 event at a time so you must bind them explicitly.

//So instead of 

google.maps.event.addListener(map, 'click dragend', function (event) { ...
// You would want to do this
google.maps.event.addListener(map, 'click', function (event) { ...
// and
google.maps.event.addListener(marker, 'dragend', function (event) { ...

Notice also that you cannot get the latLng from a dragend of the map but rather of the marker, hence in the dragend listener it is bound to the marker Have a look at the updated fiddle http://jsfiddle.net/DSc7r/1/ to see what I mean.

Upvotes: 5

Related Questions