javascript - 我如何使用google api从选定的国家/地区找到地址

标签 javascript google-places-api

目前我正在使用 google place api 获取地址 我得到了完美的

但现在我想把它限制在特定的城市

这是我的代码

// Create the autocomplete object, restricting the search to geographical
// location types.
autocomplete = new google.maps.places.Autocomplete(
  (document.getElementById('autocomplete')),
    {types: ['geocode']});

// When the user selects an address from the dropdown, populate the address
// fields in the form.
autocomplete.addListener('place_changed', fillInAddress);



 // and fill the corresponding field on the form.
        for (var i = 0; i < place.address_components.length; i++) {
          var addressType = place.address_components[i].types[0];
          if (componentForm[addressType]) {
            var val = place.address_components[i][componentForm[addressType]];
            document.getElementById(addressType).value = val;
          }
        }
      }

output 现在我想要的是,如果我从选择框中选择“INDIA”,则只有印度地址应该出现在我的文本框中

谁知道如何限制这个

最佳答案

在创建对象时添加对自动完成的限制。

var options = {
  types: ['geocode'],
  componentRestrictions: {country: "IN"}
 };

autocomplete = new google.maps.places.Autocomplete(
  (document.getElementById('autocomplete')),
    options);

// When the user selects an address from the dropdown, populate the address
// fields in the form.
autocomplete.addListener('place_changed', fillInAddress);



 // and fill the corresponding field on the form.
        for (var i = 0; i < place.address_components.length; i++) {
          var addressType = place.address_components[i].types[0];
          if (componentForm[addressType]) {
            var val = place.address_components[i][componentForm[addressType]];
            document.getElementById(addressType).value = val;
          }
        }
      }

随着国家值(value)的变化,改变值(value)

options.componentRestrictions.country

关于javascript - 我如何使用google api从选定的国家/地区找到地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49237412/

相关文章:

javascript - Firestore 抛出 'onSnapshot() requires between 1 and 4 arguments'

node.js - 在nodejs中传递下一页 token 后无法访问google place api

java - 如何在实现 Filterable 接口(interface)的 AutocompleteTextView 中获取建议之前等待用户停止输入?

google-places-api - 来自 Google Places Api 的 Photo_reference

javascript - 在 JavaScript If/else 语句中,如何根据条件呈现脚本标签?

Javascript 对象到具有多个级别的 XML 字符串

javascript - 使用 DnD 事件时 Firefox 中的 NoModificationAllowedError

javascript - 在 knockout 组件中丢失上下文

google-maps-api-3 - 什么是 Google Places API 的良好且无限制的替代方案?

ios - 地方 api 给出内部错误