9

I am using the Google Maps' Places API for autocompletion of location as user types. But how can I restrict the suggestions to a particular country?

Here is my javascript:

function initAutocomplete() {

    // Create the search box and link it to the UI element.
    var input = document.getElementById('autocomplete-input');
    var searchBox = new google.maps.places.SearchBox(input);

    // [START region_getplaces]
    // Listen for the event fired when the user selects a prediction and retrieve
    // more details for that place.
    searchBox.addListener('places_changed', function() {
        var places = searchBox.getPlaces();
        if (places.length == 0) {
            return;
        }
    });
}
Mayeenul Islam
  • 4,532
  • 5
  • 49
  • 102
Abhilash
  • 2,864
  • 3
  • 33
  • 67

2 Answers2

15

Fixed it by changing SearchBox to Autocomplete and passing an options with the requirements..Here is the code if someone needs.

function initAutocomplete() {

    // Create the search box and link it to the UI element.
    var input = document.getElementById('autocomplete-input');
    var options = {
        componentRestrictions: {country: 'fr'}
    };

    var searchBox = new google.maps.places.Autocomplete(input, options);

    // [START region_getplaces]
    // Listen for the event fired when the user selects a prediction and retrieve
    // more details for that place.
    searchBox.addListener('places_changed', function() {
        var places = searchBox.getPlaces();
        if (places.length == 0) {
            return;
        }
    });
}
Abhilash
  • 2,864
  • 3
  • 33
  • 67
  • 1
    With replacing `.SearchBox` with `.Autocomplete` the autocomplete is working fine, but `'places_changed'` not triggered when user choose a place or hit enter. – Mayeenul Islam Nov 06 '17 at 13:11
  • Posted what I found as an answer. – Mayeenul Islam Nov 07 '17 at 06:00
  • @MayeenulIslam I dont think I had the issue you mentioned(maybe there is an api change as i implemented a while back). I cant recall properly as this is a while back. My current application uses autocomplete with country restriction and working fine. I will recheck and get back as am not sure about the approach I took this time. – Abhilash Nov 07 '17 at 08:03
  • Good point about the API change. I'm using Maps API v3 (`3.30.13`). – Mayeenul Islam Nov 07 '17 at 08:31
  • I also have same problem –  Mar 23 '20 at 08:16
  • According to the documentation [link](https://developers.google.com/maps/documentation/javascript/reference/places-widget#Autocomplete.place_changed) the event name should be 'place_changed' when binding to autocomplete. So replacing 'places_changed' to 'place_changed' should work. – zeee Apr 13 '20 at 20:38
  • I have the same issue about the event binding not working. Thanks zeee for the solution, change event from places_changed to place_changed work for me. Please also take note that the function to get place is getPlace() instead of getPlaces() – TienPing Jul 30 '21 at 12:58
13

Note: I'm using Google Maps API v3 (v3.30.13)

@Abhilash's approach to the problem was nice to try with. But with his answer, I faced a problem: the autocomplete was working fine, but it didn't triggering the places_changed method. And there's no console error too.

Then with the answer by @geocodezip in another thread, I figured out that, the event fired up with Autocomplete is not places_changed, but place_changed (note the difference of singular form).

And there are other changes required if you want to proceed with place_changed, because with place_changed it return only a single place, not an array of places. So:

  • instead of .getPlaces() you have to use .getPlace()
  • instead of places.forEach(function(place) { ... } you have to work with place directly.

    var input = document.getElementById('pac-input');
    var restrictOptions = {
        componentRestrictions: {country: 'bd'}
    };
    var searchBox = new google.maps.places.Autocomplete(input, restrictOptions);
    
    searchBox.addListener('place_changed', function() {
    
        var place = searchBox.getPlace();
    
        if (place.length == 0) {
            return;
        }
    
        if (!place.geometry) {
            console.log("No details available for input: '" + place.name + "'");
            return;
        }
    
        // ...
    
    });
    

Caveat

The limitation is: with .SearchBox() google places search work even with random string, but with .Autocomplete() random string cannot find out any .geometry, hence cannot pinpoint to any matched area on the map. With .SearchBox() if you type "Name of a place", hit enter, it can point to Placename, Locality. But with .Autocomplete() without choosing from the autosuggestion[s] it cannot trigger the place's .geometry. The following console.log(place) displays the difference between random string and autocomplete chosen item:

Random place vs. autocomplete place

Aftermath

I's not satisfied with the outcome how .Autocomplete() worked for me. So until having a nicer solution, I decided to stick to the .SearchBox() method - though it's not restricting the search suggestions. But binding with bounds_changed it binds the search result a little bit, though it's not strict and satisfactory, but at least something's better than nothing. ¯\_(ツ)_/¯

// Bias the SearchBox results towards current map's viewport.
front_end_map.addListener('bounds_changed', function() {
    searchBox.setBounds(front_end_map.getBounds());
});

I, desperately am, looking forward for a bulletproof solution...

Mayeenul Islam
  • 4,532
  • 5
  • 49
  • 102
  • i am getting an error searchBox.getPlace is not a function –  Mar 25 '20 at 04:31
  • I also have same issue , the autocomplete was working fine, but it didn't triggering the places_changed method. And there's no console error too –  Mar 25 '20 at 04:32
  • 1
    +1, helps a lot. However, maybe it's since changed, but autocomplete now returns geometry. Will snap to a location on the map when you select a location. The caveat for me is that when you search a generic term, the SearchBox will create a pin for the top results but the autocomplete won't, since it expects a selected place not a vague term. – Ryan Jul 13 '21 at 11:26
  • @Ryan Please that it helped. What about adding another answer with the updates that you got with the new API? Somebody else will get help from that too. – Mayeenul Islam Jul 13 '21 at 14:14