- 首页
- 前端开发
- Google Maps API 自动完成同一页面上的第二个地址字段
Google Maps API 自动完成同一页面上的第二个地址字段
[英] Google Maps API autocomplete 2nd address fields on same page
本文介绍了Google Maps API 自动完成同一页面上的第二个地址字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在我的页面上使用 Google Maps API,该页面要求用户填写您的当前地址"和新地址".
我可以让自动完成功能在第一个地址上工作,但对第二个地址不起作用,我做了大量研究并查看了 stackoverflow 上的类似帖子,但我找不到任何遇到相同问题的人.
这是我的代码;
<input id="autocomplete" placeholder="开始输入您的地址" onFocus="geolocate()" type="text"></input>
<div id="addressone"><input type="text" id="street_number" name="street_number"></input><input type="text" id="route" name="street_name"></input><input type="text" id="locality" name="town_city"></input><input type="text" id="postal_code" name="postcode"></input><input type="text" id="country" name="country"></input>
<div id="locationField2"><input id="autocomplete2" placeholder="开始输入您的地址" onFocus="geolocate()" type="text"></input>
<div id="addresstwo"><input type="text" id="street_number2" name="street_number2"></input><input type="text" id="route2" name="street_name2"></input><input type="text" id="locality2" name="town_city2"></input><input type="text" id="postal_code2" name="postcode2"></input><input type="text" id="country2" name="country2"></input>
<脚本>//此示例使用自动完成功能显示地址表单//帮助用户填写信息的 Google Places API.var placeSearch,自动完成;var componentForm = {street_number: 'short_name',路线:'long_name',地点:'long_name',行政区域_级别_1:'short_name',国家:'long_name',postal_code: 'short_name'};函数 initAutocomplete() {//创建自动完成对象,将搜索限制为地理//位置类型.自动完成 = 新的 google.maps.places.Autocomplete(/** @type {!HTMLInputElement} */(document.getElementById('autocomplete')),{类型:['地理编码']});//当用户从下拉列表中选择一个地址时,填充该地址//表单中的字段.autocomplete.addListener('place_changed', fillInAddress);}//[开始 region_fillform]函数 fillInAddress() {//从自动完成对象中获取地点详细信息.var place = autocomplete.getPlace();for(componentForm 中的 var 组件){document.getElementById(component).value = '';document.getElementById(component).disabled = false;}//从地点详情中获取地址的各个组成部分//并填写表单上的相应字段.for (var i = 0; i < place.address_components.length; i++) {var addressType = place.address_components[i].types[0];如果(组件形式[地址类型]){var val = place.address_components[i][componentForm[addressType]];document.getElementById(addressType).value = val;}}}//[END region_fillform]//[开始 region_geolocation]//将自动完成对象偏向用户的地理位置,//由浏览器的navigator.geolocation"对象提供.功能地理定位(){如果(navigator.geolocation){navigator.geolocation.getCurrentPosition(function(position) {var 地理位置 = {纬度:位置坐标.纬度,lng: position.coords.longitude};var circle = new google.maps.Circle({中心:地理位置,半径:position.coords.accuracy});autocomplete.setBounds(circle.getBounds());});}}//[END region_geolocation]<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyC7XIOPnu4WS_fBaIDPkCBdYa3MxdIcdK4&signed_in=true&libraries=places>callback=initAutocomplete"异步延迟></script</script
解决方案
您需要处理两个自动完成输入.这是 fillInAddress
的通用版本,它将处理多个自动完成对象,这些对象具有具有唯一扩展名的字段(表单的第二个版本中的2"):
function fillInAddress(autocomplete, unique) {//从自动完成对象中获取地点详细信息.var place = autocomplete.getPlace();for(componentForm 中的 var 组件){if(!!document.getElementById(component + unique)){document.getElementById(component + unique).value = '';document.getElementById(component + unique).disabled = false;}}//从地点详情中获取地址的各个组成部分//并填写表单上的相应字段.for (var i = 0; i < place.address_components.length; i++) {var addressType = place.address_components[i].types[0];if (componentForm[addressType] && document.getElementById(addressType + unique)) {var val = place.address_components[i][componentForm[addressType]];document.getElementById(addressType + unique).value = val;}}}
这样称呼它:
//创建自动完成对象,将搜索限制为地理//位置类型.自动完成 = 新的 google.maps.places.Autocomplete(/** @type {!HTMLInputElement} */(document.getElementById('autocomplete')), {类型:['地理编码']});//当用户从下拉列表中选择一个地址时,填充该地址//表单中的字段.autocomplete.addListener('place_changed', function () {填充地址(自动完成,");});//创建第二个自动完成对象,将搜索限制为地理//位置类型.autocomplete2 = 新的 google.maps.places.Autocomplete(/** @type {!HTMLInputElement} */(document.getElementById('autocomplete2')), {类型:['地理编码']});autocomplete2.addListener('place_changed', function () {fillInAddress(autocomplete2, 2");});
工作代码片段:
//这个例子显示一个地址表单,使用自动完成功能//帮助用户填写信息的 Google Places API.var placeSearch,自动完成,自动完成2;var componentForm = {street_number: 'short_name',路线:'long_name',地点:'long_name',行政区域_级别_1:'short_name',国家:'long_name',postal_code: 'short_name'};函数 initAutocomplete() {//创建自动完成对象,将搜索限制为地理//位置类型.自动完成 = 新的 google.maps.places.Autocomplete(/** @type {!HTMLInputElement} */(document.getElementById('autocomplete')), {类型:['地理编码']});//当用户从下拉列表中选择一个地址时,填充该地址//表单中的字段.autocomplete.addListener('place_changed', function() {填充地址(自动完成,");});autocomplete2 = 新的 google.maps.places.Autocomplete(/** @type {!HTMLInputElement} */(document.getElementById('autocomplete2')), {类型:['地理编码']});autocomplete2.addListener('place_changed', function() {fillInAddress(autocomplete2, "2");});}功能fillInAddress(自动完成,唯一){//从自动完成对象中获取地点详细信息.var place = autocomplete.getPlace();for(componentForm 中的 var 组件){if (!!document.getElementById(component + unique)) {document.getElementById(component + unique).value = '';document.getElementById(component + unique).disabled = false;}}//从地点详情中获取地址的各个组成部分//并填写表单上的相应字段.for (var i = 0; i < place.address_components.length; i++) {var addressType = place.address_components[i].types[0];if (componentForm[addressType] && document.getElementById(addressType + unique)) {var val = place.address_components[i][componentForm[addressType]];document.getElementById(addressType + unique).value = val;}}}google.maps.event.addDomListener(window, "load", initAutocomplete);功能地理定位(){如果(navigator.geolocation){navigator.geolocation.getCurrentPosition(function(position) {var 地理位置 = {纬度:位置坐标.纬度,lng: position.coords.longitude};var circle = new google.maps.Circle({中心:地理位置,半径:position.coords.accuracy});autocomplete.setBounds(circle.getBounds());});}}
<script src="https://maps.googleapis.com/maps/api/js?libraries=places&key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></脚本><div id="locationField"><input id="autocomplete" placeholder="开始输入您的地址" onFocus="geolocate()" type="text"/>
<div id="addressone"><input type="text" id="street_number" name="street_number"/><input type="text" id="route" name="street_name"/><input type="text" id="locality" name="town_city"/><input type="text" id="administrative_area_level_1" name="administrative_area_level_1"/><input type="text" id="postal_code" name="postcode"/><input type="text" id="country" name="country"/>
<div id="locationField2"><input id="autocomplete2" placeholder="开始输入您的地址" onFocus="geolocate()" type="text"/>
<div id="addresstwo"><input type="text" id="street_number2" name="street_number2"/><input type="text" id="route2" name="street_name2"/><input type="text" id="locality2" name="town_city2"/><input type="text" id="administrative_area_level_12" name="administrative_area_level_12"/><input type="text" id="postal_code2" name="postcode2"/><input type="text" id="country2" name="country2"/>
I am using the Google Maps API on my page, the page asks the user to fill out your "Current Address" and the "New Address".
I can get the autocomplete to work on the 1st address but it does not work for the second address, I have done lots of research and looked at simular posts on stackoverflow but I cannot find anyone who has had the same problem.
Here is my code;
<div id="locationField">
<input id="autocomplete" placeholder="Start typing your address" onFocus="geolocate()" type="text"></input>
</div>
<div id="addressone">
<input type="text" id="street_number" name="street_number"></input>
<input type="text" id="route" name="street_name"></input>
<input type="text" id="locality" name="town_city"></input>
<input type="text" id="postal_code" name="postcode"></input>
<input type="text" id="country" name="country"></input>
</div>
<div id="locationField2">
<input id="autocomplete2" placeholder="Start typing your address" onFocus="geolocate()" type="text"></input>
</div>
<div id="addresstwo">
<input type="text" id="street_number2" name="street_number2"></input>
<input type="text" id="route2" name="street_name2"></input>
<input type="text" id="locality2" name="town_city2"></input>
<input type="text" id="postal_code2" name="postcode2"></input>
<input type="text" id="country2" name="country2"></input>
</div>
<script>
// This example displays an address form, using the autocomplete feature
// of the Google Places API to help users fill in the information.
var placeSearch, autocomplete;
var componentForm = {
street_number: 'short_name',
route: 'long_name',
locality: 'long_name',
administrative_area_level_1: 'short_name',
country: '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('autocomplete')),
{types: ['geocode']});
// When the user selects an address from the dropdown, populate the address
// fields in the form.
autocomplete.addListener('place_changed', fillInAddress);
}
// [START region_fillform]
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;
}
}
}
// [END region_fillform]
// [START region_geolocation]
// 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());
});
}
}
// [END region_geolocation]
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyC7XIOPnu4WS_fBaIDPkCBdYa3MxdIcdK4&signed_in=true&libraries=places&callback=initAutocomplete" async defer></script>
</div>
解决方案
You need to hande the two autocomplete inputs. Here is a generalized version of fillInAddress
that will handle multiple autocomplete objects with fields with a unique extension (the "2" in your second version of the form):
function fillInAddress(autocomplete, unique) {
// Get the place details from the autocomplete object.
var place = autocomplete.getPlace();
for (var component in componentForm) {
if(!!document.getElementById(component + unique)){
document.getElementById(component + unique).value = '';
document.getElementById(component + unique).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] && document.getElementById(addressType + unique)) {
var val = place.address_components[i][componentForm[addressType]];
document.getElementById(addressType + unique).value = val;
}
}
}
Call it like this:
// Create the autocomplete object, restricting the search to geographical
// location types.
autocomplete = new google.maps.places.Autocomplete(
/** @type {!HTMLInputElement} */
(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', function () {
fillInAddress(autocomplete, "");
});
// Create the second autocomplete object, restricting the search to geographical
// location types.
autocomplete2 = new google.maps.places.Autocomplete(
/** @type {!HTMLInputElement} */
(document.getElementById('autocomplete2')), {
types: ['geocode']
});
autocomplete2.addListener('place_changed', function () {
fillInAddress(autocomplete2, "2");
});
working code snippet:
// This example displays an address form, using the autocomplete feature
// of the Google Places API to help users fill in the information.
var placeSearch, autocomplete, autocomplete2;
var componentForm = {
street_number: 'short_name',
route: 'long_name',
locality: 'long_name',
administrative_area_level_1: 'short_name',
country: '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('autocomplete')), {
types: ['geocode']
});
// When the user selects an address from the dropdown, populate the address
// fields in the form.
autocomplete.addListener('place_changed', function() {
fillInAddress(autocomplete, "");
});
autocomplete2 = new google.maps.places.Autocomplete(
/** @type {!HTMLInputElement} */
(document.getElementById('autocomplete2')), {
types: ['geocode']
});
autocomplete2.addListener('place_changed', function() {
fillInAddress(autocomplete2, "2");
});
}
function fillInAddress(autocomplete, unique) {
// Get the place details from the autocomplete object.
var place = autocomplete.getPlace();
for (var component in componentForm) {
if (!!document.getElementById(component + unique)) {
document.getElementById(component + unique).value = '';
document.getElementById(component + unique).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] && document.getElementById(addressType + unique)) {
var val = place.address_components[i][componentForm[addressType]];
document.getElementById(addressType + unique).value = val;
}
}
}
google.maps.event.addDomListener(window, "load", initAutocomplete);
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 src="https://maps.googleapis.com/maps/api/js?libraries=places&key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<div id="locationField">
<input id="autocomplete" placeholder="Start typing your address" onFocus="geolocate()" type="text" />
</div>
<div id="addressone">
<input type="text" id="street_number" name="street_number" />
<input type="text" id="route" name="street_name" />
<input type="text" id="locality" name="town_city" />
<input type="text" id="administrative_area_level_1" name="administrative_area_level_1" />
<input type="text" id="postal_code" name="postcode" />
<input type="text" id="country" name="country" />
</div>
<div id="locationField2">
<input id="autocomplete2" placeholder="Start typing your address" onFocus="geolocate()" type="text" />
</div>
<div id="addresstwo">
<input type="text" id="street_number2" name="street_number2" />
<input type="text" id="route2" name="street_name2" />
<input type="text" id="locality2" name="town_city2" />
<input type="text" id="administrative_area_level_12" name="administrative_area_level_12" />
<input type="text" id="postal_code2" name="postcode2" />
<input type="text" id="country2" name="country2" />
</div>
这篇关于Google Maps API 自动完成同一页面上的第二个地址字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文