1
votes

I am developping a website offering home delivrery in Belgium and in France.

In this website, there is a search input using the autocomplete addressform from Google (using Google Map API + Place library). I took the example: https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete-addressform?hl=fr

It works very well but when I type the first letters into the input, it shows results (postal addresses) from USA, Canada, Spain, ect.

It does not make sense for this project. It should show only results from Belgium or France (or eventually, results from Belgium/France should be shown first).

I know there is an option to limit the results to a specific country but this is not the solution. I can not restrict the results to only 'France' or only 'Belgium'

What should I do ?

Thanks you a lot

1
This is exactly what I needed ! Thanks you. But why -1 to my question ? This is not respectful. This is not a stupid question. - Kreeger
Really? Please do not be irrespectful. I waste a lot of time reading documentation and testing samples. Your link does not appear in my Google results. And tell me why "Place Autocomplete restricted to multiple countries" is missing in the menu in my language ? What you see: hpics.li/d1ae6e1 What I see: hpics.li/c599c95 - Kreeger

1 Answers

2
votes

I couldn't quite tell from the comments whether or not you were able to get this sorted out, so I'll post my answer.

Once you have an autocomplete object, you just need to set the component restrictions. Here is how I have done this:

var autocomplete = new google.maps.places.Autocomplete(input);

// Set initial restrict to the greater list of countries.
autocomplete.setComponentRestrictions({'country': ['FR', 'BE']});

I have a working JSFiddle with these restrictions in place that is restricting results to Belgium and France like you were wanting. Please be advised that you will need to put your own API key in this for it to work as I have redacted mine for security:

https://jsfiddle.net/ztqgc4tw/

I hope this helps!