gpt4 book ai didi

javascript - Google API 自动完成

转载 作者:行者123 更新时间:2023-12-01 03:20:06 24 4
gpt4 key购买 nike

我遇到了一个简单的问题。我可以使用 google API 并获取地址,但我不想将街道号码和街道地址放在单独的框中,我希望将街道号码与街道地址组合并放置在同一个框中。任何指示或建议将不胜感激。

当前结果; enter image description here

这就是我想要的最终结果; enter image description here

我的代码;

HTML

   <body>
<div class='container'>
<div class='row'>
<h2>Example</h2>
<p>User enters: "3 Dennis", a list of available options appears:</p>
<ul>
<li>3 Dennis Vale Drive, Daisy Hill, Queensland, Australia</li>
<li>etc.</li>
</ul>
</div>
<div class='row'>
<div class='col-xs-4 text-right'>Address</div>
<div class='col-xs-6'><input type='text' class='form-control address-field' id='street_number'></div>
</div>
<div class='row'>
<div class='col-xs-4 text-right'>Suburb</div>
<div class='col-xs-6'><input type='text' class='form-control suburb-field' id='locality'></div>
</div>
<div class='row'>
<div class='col-xs-4 text-right'>State</div>
<div class='col-xs-6'><input type='text' class='form-control state-field' id='administrative_area_level_1'></div>
</div>
<div class='row'>
<div class='col-xs-4 text-right'>Post Code</div>
<div class='col-xs-6'><input type='text' class='form-control postcode-field' id='postal_code'></div>
</div>
</div>

Javascript

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

function initAutocomplete() {
// Create the autocomplete object, restricting the search to geographical
// location types.
autocomplete = new google.maps.places.Autocomplete(
/** @type {!HTMLInputElement} */(document.getElementById('street_number')),
{types: ['geocode']});

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

function fillInAddress() {
// Get the place details from the autocomplete object.
var place = autocomplete.getPlace();

for (var component in componentForm) {
document.getElementById(component).value = '';
document.getElementById(component).disabled = false;
}

// Get each component of the address from the place details
// 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;
}
}
}

// Bias the autocomplete object to the user's geographical location,
// as supplied by the browser's 'navigator.geolocation' object.
function geolocate() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var geolocation = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
var circle = new google.maps.Circle({
center: geolocation,
radius: position.coords.accuracy
});
autocomplete.setBounds(circle.getBounds());
});
}
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyD_Qf8TYV9CLEngR4Fioj-S9_QVX9amC7Y&libraries=places&callback=initAutocomplete"
async defer></script>

最佳答案

您可以在 componentForm 字典 中添加一个名为 route 的新组件

var componentForm = {
street_number: 'short_name',
locality: 'long_name',
administrative_area_level_1: 'long_name',
postal_code: 'short_name',
route:'long_name'
};

route 属性包含street_address

long_name:"Dennis Vale Drive"

route 属性可以存储在隐藏 输入中。

<input id="route" style="display:none"/>

完成填充所有输入后,只需连接 street_number 输入和 route 输入中的值即可。元素。

document.getElementById('street_number').value=document.getElementById('street_number').value+' '+document.getElementById('route').value;

这里是full solution.

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

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