在国家/地区,印度仅对浦那市限制自动搜索。我们怎样才能做到这一点?

我试过了

autocomplete.setComponentRestrictions(
      {'country': ['in']},{'city':['Pune','Mumbai']});

最佳答案

试试这个可能会有所帮助

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


或者你可以尝试

var input = document.getElementById('searchCity');
var options = { types: ['(cities)'],componentRestrictions: {country: 'us' }};
var autoComplete = new google.maps.places.Autocomplete(cityInput,options);`


自己做的更有效的方法

function InitializeFilter() {

    var iso           = ['AD','AE','AF','AG','AI','AL','AM','AO','AQ','AR','AS','AT','AU','AW','AX','AZ','BA','BB','BD','BE','BF','BG','BH','BI','BJ','BL','BM','BN','BO','BQ','BR','BS','BT','BV','BW','BY','BZ','CA','CC','CD','CF','CG','CH','CI','CK','CL','CM','CN','CO','CR','CU','CV','CW','CX','CY','CZ','DE','DJ','DK','DM','DO','DZ','EC','EE','EG','EH','ER','ES','ET','FI','FJ','FK','FM','FO','FR','GA','GB','GD','GE','GF','GG','GH','GI','GL','GM','GN','GP','GQ','GR','GS','GT','GU','GW','GY','HK','HM','HN','HR','HT','HU','ID','IE','IL','IM','IN','IO','IQ','IR','IS','IT','JE','JM','JO','JP','KE','KG','KH','KI','KM','KN','KP','KR','KW','KY','KZ','LA','LB','LC','LI','LK','LR','LS','LT','LU','LV','LY','MA','MC','MD','ME','MF','MG','MH','MK','ML','MM','MN','MO','MP','MQ','MR','MS','MT','MU','MV','MW','MX','MY','MZ','NA','NC','NE','NF','NG','NI','NL','NO','NP','NR','NU','NZ','OM','PA','PE','PF','PG','PH','PK','PL','PM','PN','PR','PS','PT','PW','PY','QA','RE','RO','RS','RU','RW','SA','SB','SC','SD','SE','SG','SH','SI','SJ','SK','SL','SM','SN','SO','SR','SS','ST','SV','SX','SY','SZ','TC','TD','TF','TG','TH','TJ','TK','TL','TM','TN','TO','TR','TT','TV','TW','TZ','UA','UG','UM','US','UY','UZ','VA','VC','VE','VG','VI','VN','VU','WF','WS','YE','YT','ZA','ZM','ZW'];
        goo           =  google.maps,
        input         = document.getElementById('searchCity'),
        country       = document.getElementById('shortNameCountry'),
        options       = {types: ['(cities)']},
        autoComplete  = new google.maps.places.Autocomplete(input,options);

    goo.event.addDomListener(country,'input',function(){
      var val=this.value.trim().toUpperCase();
      if(iso.indexOf(val)>-1){
        this.style.background='white';
        input.value=' ';
        autoComplete
        .setComponentRestrictions({country:val});

      }
      else{
        this.style.background='red';
      }
    });
    goo.event.trigger(country,'input');

  }
  google.maps.event.addDomListener(window, 'load', InitializeFilter);


欲了解更多信息,请访问here

关于javascript - 在谷歌 map 中如何限制自动搜索印度的单个城市,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52847536/

10-11 12:27