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

前端 未结 4 1606
逝去的感伤
逝去的感伤 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:00

    Google is not providing autocomplete for particular city but we can do it manually. in my case i want to display address of 'Maharashtra' state.

      this.GoogleAutocomplete = new google.maps.places.AutocompleteService();
    
        this.GoogleAutocomplete.getPlacePredictions(
      {
        input: this.autocompleteFrom.input,
        types: ['geocode'],
        componentRestrictions: { country: 'in' },
      },
      (predictions, status) => {
        this.autocompleteItemsFrom = [];
        this.zone.run(() => {
          if (predictions) {
            console.log(predictions)
            predictions.forEach((prediction) => {
              var statearray = prediction.description.split(",")
              var state = statearray[statearray.length - 2]
              if(state === ' Maharashtra'){
                this.autocompleteItemsFrom.push(prediction);
              }
            });
          }
        });
      });
    

    i hope it will help you.

提交回复
热议问题