gpt4 book ai didi

javascript - Google map API - 自动完成英国县

转载 作者:行者123 更新时间:2023-11-28 04:25:18 27 4
gpt4 key购买 nike

我正在使用 Google 地址自动完成 API 来格式化内部应用的地址,并使用 Google 提供的标准示例代码:

https://google-developers.appspot.com/maps/documentation/javascript/examples/full/places-autocomplete-addressform

对于每个邮政编码,我正确地得到了类似于以下内容的响应:

   "html_attributions" : [],
"result" : {
"address_components" : [
{
"long_name" : "BA1 1UN",
"short_name" : "BA1 1UN",
"types" : [ "postal_code" ]
},
{
"long_name" : "Bath",
"short_name" : "Bath",
"types" : [ "postal_town" ]
},
{
"long_name" : "Bath and North East Somerset",
"short_name" : "Bath and North East Somerset",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "England",
"short_name" : "England",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United Kingdom",
"short_name" : "GB",
"types" : [ "country", "political" ]
}

除了“administrative_area_level_2”(即县)之外,我的所有表单字段都填写得很好。我的工作 HTML 如下所示:

<div class="form-group">
<label>Postal Town</label>
<input class="form-control field" placeholder="Enter your country"
required="true" name="postal_town" id="postal_town">
<p class="help-block">Enter your country.</p>
</div>

但这并不:

<div class="form-group">
<label>County</label>
<input class="form-control field" placeholder="Enter your county" required="true" name="administrative_area_level_2" id="administrative_area_level_2">
<p class="help-block">Enter your county.</p>
</div>

如何访问该字段以自动填写表单?

最佳答案

有时走开并重新审视自己是件好事。修复非常简单,标准的 Google 自动完成功能具有以下代码:

          var placeSearch, autocomplete;
var componentForm = {
street_number: 'short_name',
route: 'long_name',
postal_town: 'long_name',
administrative_area_level_1: 'short_name',
country: 'long_name',
postal_code: 'short_name'
};

您只需包含附加字段,以便代码显示:

          var placeSearch, autocomplete;
var componentForm = {
street_number: 'short_name',
route: 'long_name',
postal_town: 'long_name',
administrative_area_level_1: 'short_name',
administrative_area_level_2: 'short_name',
country: 'long_name',
postal_code: 'short_name'
};

现在它会自动填充表单,我还没有尝试过,但假设它也适用于其他字段。喜欢谷歌!

关于javascript - Google map API - 自动完成英国县,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45117757/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com