Running autocomplete in Google Places Places places changed when text enters one value of one of the selected

I am using Google Places Autocomplete API and jQuery. Basic functionality works fine. I would like to be done in case someone types something in autocomplete, which is exactly the same as one of the options I would like to select and the event place_changed

to trigger. This is because I have some kind of logic in the places where the listener I need to call has changed. The idea is that if someone enters the same text as one of the autocomplete options, they should act as if someone had clicked on that option.

This is what I tried, where userLocation is the input:

$("#userLocation").keyup(function(e){
    //if the text is at least 4 chars long
    if($("#userLocation").val().length > 4)
    {
        //iterate through each of the autocomplete options
        $(".pac-item").each(function(){
            if($(this).text() == $("#userLocation").val() )
            {
                //does not do what I need it to
                $(this).trigger("place_changed");
            }
        });
    }
});

      

I've also tried replacing trigger("place_changed")

with .trigger("click")

, but to no avail.

Has anyone done this before? Can anyone suggest another way to try this work? Any advice would be greatly appreciated, thanks!

+3


source to share


2 answers


As mentioned in the answer here :

You have to call a function google.maps.event.trigger

that will result in a call like below.



$("#userLocation").keyup(function(e){
    //if the text is at least 4 chars long
    if($("#userLocation").val().length > 4)
    {
        //iterate through each of the autocomplete options
        $(".pac-item").each(function(){
            if($(this).text() == $("#userLocation").val() )
            {
                google.maps.event.trigger(autocomplete, 'place_changed');
            }
        });
    }
});

      

The documentation shows that you can also pass arguments to a function trigger

that will be passed to the event listener for the 'place_changed' event.

+4


source


you can also try using autocomplete, enter a key event and fire the trigger using javascript.

Please try the following code.



//For Set Location pin point
var searchtext = "New York, NY";
var input = document.getElementById('pac-input');
$('#pac-input').val(searchtext);                        
setTimeout(function () {
google.maps.event.trigger(input, 'focus');
google.maps.event.trigger(input, 'keydown', { keyCode: 13 });                            
}, 10);
//End 

      

0


source







All Articles