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)
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.
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.
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.
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.
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.
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 .
Adapted from Jonathan Caulfield's answer:
$('.search-location').keypress(function(e) { if (e.which == 13) { google.maps.event.trigger(autocomplete, 'place_changed'); return false; } });
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With