admin管理员组

文章数量:1330427

Following the guide found I am trying to filter my search box to only show suggestions for the UK.

I have the code:

<script src=";libraries=places&callback=initialize&v=3"
        async defer></script>


<script>

    function initialize() {
        setTimeout(function() {
            initMap();
            initAutoplete();
        }, 4000);

    }

    function initAutoplete() {

        var options = {
            types: [],
            ponentRestrictions: {country: 'GB' }
        };

        var input = document.getElementById('pac-input');
        var searchBox = new google.maps.places.SearchBox(input, options);

    }

</script>

Which works to the point that the search box gets initialised and is usable however its not filtering to just UK locations. The box will show locations remendations for the world over.

Using GB or gb gives the same result

Following the guide found https://developers.google./maps/documentation/javascript/places-autoplete#places_searchbox I am trying to filter my search box to only show suggestions for the UK.

I have the code:

<script src="https://maps.googleapis./maps/api/js?key=XXXXXXX&libraries=places&callback=initialize&v=3"
        async defer></script>


<script>

    function initialize() {
        setTimeout(function() {
            initMap();
            initAutoplete();
        }, 4000);

    }

    function initAutoplete() {

        var options = {
            types: [],
            ponentRestrictions: {country: 'GB' }
        };

        var input = document.getElementById('pac-input');
        var searchBox = new google.maps.places.SearchBox(input, options);

    }

</script>

Which works to the point that the search box gets initialised and is usable however its not filtering to just UK locations. The box will show locations remendations for the world over.

Using GB or gb gives the same result

Share edited Jul 16, 2018 at 18:20 DevWithZachary asked Jul 16, 2018 at 18:12 DevWithZacharyDevWithZachary 3,68511 gold badges52 silver badges106 bronze badges 2
  • Their example (developers.google./maps/documentation/javascript/…) says lowercase country: ponentRestrictions: {country: 'fr'}. Try lowercasing it? – xrd Commented Jul 16, 2018 at 18:16
  • Can we filter by City addresses? – Sangwin Gawande Commented Sep 21, 2022 at 14:22
Add a ment  | 

4 Answers 4

Reset to default 4

In the end switching to

function initAutoplete() {
        var input = document.getElementById('pac-input');
        var options = {
            types: ['(cities)'],
            ponentRestrictions: {country: 'GB'}
            };

        var autoplete = new google.maps.places.Autoplete(input, options);
    }

fixed my issue

Maybe try setComponentRestrictions method.

var options = {
  types: []
};

var input = document.getElementById('pac-input');
var searchBox = new google.maps.places.SearchBox(input, options);
// This method.
searchBox.setComponentRestrictions({'country': ['gb']});

Try with the country lowercased:

<script src="https://maps.googleapis./maps/api/js?key=XXXXXXX&libraries=places&callback=initialize&v=3"
        async defer></script>


<script>
  document.onload = function initialize() {
      initMap();
      initAutoplete();
    }

    function initAutoplete() {

        var options = {
            types: [],
            ponentRestrictions: {country: 'gb' }
        };

        var input = document.getElementById('pac-input');
        var searchBox = new google.maps.places.SearchBox(input, options);

    }

</script>

If using api directly add &ponents=country:uk

e.g: https://developers.google./maps/documentation/places/web-service/autoplete

本文标签: javascriptFilter Google Places API for only one countryStack Overflow