【问题标题】:How can we restrict google map autocomplete to only a particular city?我们如何将谷歌地图自动完成限制为仅特定城市?
【发布时间】:2017-04-12 02:31:09
【问题描述】:

我的要求是只为班加罗尔的地点获取谷歌地点自动完成建议,但如果我搜索班加罗尔以外的任何其他地点,那么我也会得到我不想要的建议。我发现了许多与我的问题相关的 stackoverflow 链接,但没有一个能解决我的问题。

这可以得到任何特定城市的建议吗?

我在下面分享我的代码:-

var input = (document.getElementById('address'));
          var s_w = new google.maps.LatLng( 12.97232, 77.59480 ); //southwest
          var n_e = new google.maps.LatLng( 12.89201, 77.58905 ); //northeast
          var b_bounds = new google.maps.LatLngBounds( s_w, n_e ); //bangalorebounds

var options = {
            bounds:b_bounds,
            componentRestrictions: {country: "in"}
           };
var autocomplete = new google.maps.places.Autocomplete(input, options);

autocomplete.bindTo('bounds', map);

autocomplete.addListener('place_changed', function() {

  //rest_of_the_code

});

请有人帮忙!!

【问题讨论】:

    标签: javascript google-maps google-maps-api-3 autocomplete


    【解决方案1】:

    我觉得你可以试试这个:

    var cityBounds = new google.maps.LatLngBounds(
      new google.maps.LatLng(25.341233, 68.289986),
      new google.maps.LatLng(25.450715, 68.428345));
    
    var options = {
      bounds: cityBounds,
      types: ['geocode'],
      componentRestrictions: {country: 'est'}
    };
    

    LatLng 值替换为您的值,将country 替换为您的国家/地区代码。

    【讨论】:

    【解决方案2】:

    您可以使用边界来调整结果,但目前在自动完成的地方没有严格的城市过滤器。

    您可以在公共问题跟踪器中找到添加更严格过滤器的功能请求:

    https://code.google.com/p/gmaps-api-issues/issues/detail?id=4433

    请对此功能请求加注星标以表达您的兴趣并接收来自 Google 的更新。

    【讨论】:

      【解决方案3】:

      像这样试试。

      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 () {
      
      });
      

      注意:网址应为:https://maps.googleapis.com/maps/api/js?libraries=places&key=YOUR_API_KEY

      strictBounds option 已添加到 Maps JavaScript API 3.27 版中,目前(2017 年 1 月)是实验版本。

      【讨论】:

        【解决方案4】:

        Google 不为特定城市提供自动完成功能,但我们可以手动完成。就我而言,我想显示“马哈拉施特拉邦”的地址。

          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);
                  }
                });
              }
            });
          });
        

        希望对你有帮助。

        【讨论】:

          猜你喜欢
          • 2018-12-08
          • 2012-12-28
          • 1970-01-01
          • 2016-01-17
          • 2015-10-10
          • 2012-01-07
          • 2018-03-24
          • 1970-01-01
          • 2018-12-21
          相关资源
          最近更新 更多