Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Places Autocomplete Force Selection

I am using the JavaScript Google places autocomplete API v3. It works fine but I was wondering if there was a way to force the selection from the autocomplete, that is input that will not accept any free form text. I looked at the docs and didn't see such an option but figured I would ask just to be safe. I'm sure I could work out a way to do it with some JavaScript but would prefer to use an already built method if it is available. Thanks!

like image 524
TheMethod Avatar asked Feb 01 '13 18:02

TheMethod


People also ask

How do I restrict Google Maps autocomplete to certain cities?

It is currently not possible to restrict results to a specific locality. You can use bounds as you have done so above to bias results towards, but not restricted to places contained within the specified bounds. If you believe restriction by locality would be a useful feature please file a Places API - Feature Request.

How do I use Google Places autocomplete API?

Ensure that the API key(s) used for all Place Autocomplete and Place Details requests within a session belong to the same Cloud Console project. Be sure to pass a unique session token for each new session. Using the same token for more than one session will result in each request being billed individually.

How would you implement Google Places autocomplete programmatically?

To launch the autocomplete widget using an intent, follow these steps: Use Autocomplete. IntentBuilder to create an intent, passing the desired Autocomplete mode. The intent must call startActivityForResult , passing in a request code that identifies the intent.


2 Answers

Actually, what we did was the following:
- Every time a location is picked from the Auto complete list, we populate some hidden fields with some fields coming from the JSON response (city name, country name, longitude and latitude)
- When the form is submitted, we check if these fields have values, if they don't, it means that the user instead of selecting a location from the list, he just entered a value himself.

It is not solving the problem in a JS way, but still, it does the trick just fine!

like image 154
Iraklis Alexopoulos Avatar answered Sep 23 '22 21:09

Iraklis Alexopoulos


 <script src="http://maps.googleapis.com/maps/api/js?sensor=false&amp;libraries=places"     type="text/javascript"></script> <script>     var IsplaceChange = false;     $(document).ready(function () {                     var input = document.getElementById('txtlocation');         var autocomplete = new google.maps.places.Autocomplete(input, { types: ['(cities)'] });          google.maps.event.addListener(autocomplete, 'place_changed', function () {             var place = autocomplete.getPlace();              IsplaceChange = true;         });          $("#txtlocation").keydown(function () {             IsplaceChange = false;         });          $("#btnsubmit").click(function () {              if (IsplaceChange == false) {                 $("#txtlocation").val('');                 alert("please Enter valid location");             }             else {                 alert($("#txtlocation").val());             }          });     }); </script> 




like image 24
Digvijaysinh Zala Avatar answered Sep 20 '22 21:09

Digvijaysinh Zala