Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fire place_changed event for Google places auto-complete on Enter key

The click seems to fire the event and set the cookies but pressing enter to submit doesn't set the cookies and instead the page redirects without the cookies.

function locationAuto() {         $('.search-location').focus(function () {         autocomplete = new google.maps.places.Autocomplete(this);         searchbox = this;          google.maps.event.addListener(autocomplete, 'place_changed', function () {             var thisplace = autocomplete.getPlace();             if (thisplace.geometry.location != null) {                 $.cookie.raw = true;                 $.cookie('location', searchbox.value, { expires: 1 });                 $.cookie('geo', thisplace.geometry.location, { expires: 1 });             }         }); }); 

The .search-location is a class on multiple textboxes. There is a submit button that takes the values from the cookies and redirects (server side)

like image 768
John Stephenson Avatar asked Oct 10 '12 09:10

John Stephenson


People also ask

How do I create an autocomplete search box in Google Maps?

Google Map with Autocomplete Search BoxCreate an input element ( searchInput ) where the autocomplete addresses search will be added. Create a DIV element ( map ) to display the Google Map. Specify the elements to display the geolocation data.

What is Google Place autocomplete?

The Place Autocomplete service is a web service that returns place predictions in response to an HTTP request. The request specifies a textual search string and optional geographic bounds.

How to add Google Places autocomplete to a textbox?

The very first thing is to inherit the Google Maps API script along with the places library. Then inside the Google Maps window load event handler, the Google Places Autocomplete is applied to the TextBox and then the place_changed event handler is assigned to it.

What is the use of place_changed event in Google Places?

The place_changed event handler is triggered when a place is selected in the Google Places Autocomplete TextBox. It first gets the selected place and then determines its address and its location coordinates i.e. Latitude and Longitude and then displays the details in JavaScript alert message box.

How do I use place autocomplete with place details?

Use session-based Place Autocomplete with Place Details. Session management is automatically built into the JavaScript, Android, or iOS widgets. This includes both the Place Autocomplete requests and the Place Details request on the selected prediction.

How do I use place autocomplete with geocoding?

If Place Autocomplete is accompanied by a map, you can bias location by map viewport. If you expect the user to enter only address information, re-use the original user input in a call to the Geocoding API. If you expect the user to enter queries for a specific place by name or address, use a Find Place request .


2 Answers

Adapted from Jonathan Caulfield's answer:

$('.search-location').keypress(function(e) {   if (e.which == 13) {     google.maps.event.trigger(autocomplete, 'place_changed');     return false;   } }); 
like image 170
Marcelo Avatar answered Sep 23 '22 15:09

Marcelo


I've encountered this problem as well, and came up with a good solution. In my website I wanted to save the autocomplete.getPlace().formatted_address in a hidden input prior to submission. This worked as expected when clicking the form's submit button, but not when pressing the Enter key on the selection in the autocomplete's dropdown menu. My solution was as follows:

$(document).ready(function() {      // Empty the value on page load     $("#formattedAddress").val("");     // variable to indicate whether or not enter has been pressed on the input     var enterPressedInForm = false;      var input = document.getElementById("inputName");     var options = {       componentRestrictions: {country: 'uk'}     };     autocomplete = new google.maps.places.Autocomplete(input, options);      $("#formName").submit(function(e) {         // Only submit the form if information has been stored in our hidden input         return $("#formattedAddress").val().length > 0;     });      $("#inputName").bind("keypress", function(e) {         if(e.keyCode == 13) {             // Note that simply triggering the 'place_changed' event in here would not suffice, as this would just create an object with the name as typed in the input field, and no other information, as that has still not been retrieved at this point.              // We change this variable to indicate that enter has been pressed in our input field             enterPressedInForm = true;         }     });      // This event seems to fire twice when pressing enter on a search result. The first time getPlace() is undefined, and the next time it has the data. This is why the following logic has been added.     google.maps.event.addListener(autocomplete, 'place_changed', function () {         // If getPlace() is not undefined (so if it exists), store the formatted_address (or whatever data is relevant to you) in the hidden input.         if(autocomplete.getPlace() !== undefined) {             $("#formattedAddress").val(autocomplete.getPlace().formatted_address);         }         // If enter has been pressed, submit the form.         if(enterPressedInForm) {             $("#formName").submit();         }     }); }); 

This solution seems to work well.

like image 29
Liran H Avatar answered Sep 25 '22 15:09

Liran H