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!
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.
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.
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.
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!
<script src="http://maps.googleapis.com/maps/api/js?sensor=false&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>
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