How can we restrict google map autocomplete to only a particular city?

前端 未结 4 1598
逝去的感伤
逝去的感伤 2020-12-11 13:45

My requirement is to get google places autocomplete suggestion only for the places in Bangalore, but if I search for any other place apart from bangalore then also I get the

4条回答
  •  [愿得一人]
    2020-12-11 14:02

    Try like this.

    var bangaloreBounds = new google.maps.LatLngBounds(
       new google.maps.LatLng(12.864162, 77.438610),
       new google.maps.LatLng(13.139807, 77.711895));
    
    var autocomplete = new google.maps.places.Autocomplete(this, {
       bounds: bangaloreBounds,
       strictBounds: true,
    });
    
    autocomplete.addListener('place_changed', function () {
    
    });
    

    Note: The URL should be: https://maps.googleapis.com/maps/api/js?libraries=places&key=YOUR_API_KEY

    strictBounds option was added in version 3.27 of Maps JavaScript API which is currently (January 2017) the experimental version.

提交回复
热议问题