Deepak Shinde Deepak Shinde - 12 days ago 8
Javascript Question

Bias results of place autocomplete for selected city

I have a drop-down menu for selecting city and an autocomplete text-box. I want only locations form selected city in drop-down.

function initAutocomplete() {
autocomplete = new google.maps.places.Autocomplete(
(document.getElementById('auto')),{
bounds: new google.maps.Circle({
center: new google.maps.LatLng( 18.520430,73.856744 ),
radius: 500
}).getBounds(),
types: ['establishment']
});

autocomplete.addListener('place_changed', function(){});
}


I use above code for place autocomplete, but I want to dynamically pass longitude and latitude to this function. How can I do that? Please help me.
I'd like to have functionality like swiggy.com .

Answer

When new city is selected and you have new lat,lng of that city, use this piece of code to update the bound of autocomplete widget with new value of lat and lng:

var bounds = new google.maps.LatLngBounds();
var latLng = new google.maps.LatLng(lat, lng);
bounds.extend(latLng);
autocomplete.setBounds(bounds);