简体   繁体   中英

Google Maps API - place_changed Listener Firing On Second Click

JS

var location_input = document.getElementById('location');
var autocomplete = new google.maps.places.Autocomplete(location_input);

google.maps.event.addListener(autocomplete, 'place_changed', function(){
  alert('here');
  console.log('here');
});

HTML

{{Form::input('text', 'location', null, ['id' => 'location', 'class' => 'form-control']) }}

@section('after-scripts')
    {!! Html::script('PATH_TO_SCRIPT.js') !!}
@endsection

The event is firing if I arrow down the results and press enter. It also works if I click an Item from the list, click back into the input box, and click the same item again.

The issue I am having is the event should be firing on the first click of an item being selected and not the second.

This was a silly mistake on my end. I was using the Autocomplete method on the same text field in a different location from where I was working with the code mentioned in the question. Once I removed the 2nd Autocomplete everything started working as expected.

It's amazing what a little sleep can do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM