Answer the question
In order to leave comments, you need to log in
How to update AutoComplete search bounds in google maps api?
Hello! I use AutoComplete for hints when entering an address. There is also a list of cities, which is listed in the select(#city) tag. We need to update the AutoComplete search boundary according to the selected city. For example, if I select "Moscow", then AutoComplete should show only addresses in Moscow.
I tried to get the coordinates of the selected city when choosing select and write them to a variable, but it doesn’t work that way.
Here is the code
$("#city").change(function() {
geocodeAddress($(this).val(), function(result) {
cityForAuto = result[0]+", "+result[1];
});
});
$(".form_order").on("focus", ".plus input, #from", function() {
var input_id = $(this).attr("id");
var defaultBounds = new google.maps.LatLngBounds(
new google.maps.LatLng(cityForAuto),
);
var input = document.getElementById(input_id);
var options = {bounds: defaultBounds, strictBounds: true};
const autocomplete = new google.maps.places.Autocomplete(input, options);
autocomplete.bindTo("bounds", map);
// ...
});
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question