我正在尝试从Google地图自动填充中获取完整的地址,但是它不起作用。查看我的代码和控制台
var placeSearch, autocomplete;
var componentForm = [
'delivery-street',
'delivery-suburb',
'delivery-postcode',
'delivery-city',
'delivery-country'
];
window.initAutocomplete = function() {
// 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);
}
function fillInAddress() {
// Get the place details from the autocomplete object.
var place = autocomplete.getPlace();
console.log(place);
for (var component in componentForm) {
document.getElementById(componentForm[component]).value = '';
document.getElementById(componentForm[component]).disabled = false;
}
// Get each component of the address from the place details
// and fill the corresponding field on the form.
document.getElementById('delivery-street').value = findAttr(place, ['street_number']) + ' ' + findAttr(place, ['route']);
document.getElementById('delivery-suburb').value = findAttr(place, ['sublocality_level_1', 'locality']);
document.getElementById('delivery-postcode').value = findAttr(place, ['postal_code']);
document.getElementById('delivery-city').value = findAttr(place, ['administrative_area_level_1', 'route']);
document.getElementById('delivery-country').value = findAttr(place, ['country']);
}
function findAttr(place, findType) {
var response = [];
for (var j = 0; j < findType.length; j++) {
for (var i = 0; i < place.address_components.length; i++) {
var addressType = place.address_components[i].types[0];
if(addressType === findType[j]){
response.push(place.address_components[i].long_name);
}
}
}
return response.join(', ')
}
// Bias the autocomplete object to the user's geographical location,
// as supplied by the browser's 'navigator.geolocation' object.
window.geolocate = function () {
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());
});
}
}
因此,我输入了“ 2/89 Union St,New Brighton,Christchurch,New Zealand”,这就是控制台中的结果
address_components: Array(7)
0: {long_name: "89", short_name: "89", types: Array(1)}
1: {long_name: "Union Street", short_name: "Union St", types: Array(1)}
2: {long_name: "New Brighton", short_name: "New Brighton", types: Array(3)}
3: {long_name: "Christchurch", short_name: "Christchurch", types: Array(2)}
4: {long_name: "Canterbury", short_name: "Canterbury", types: Array(2)}
5: {long_name: "New Zealand", short_name: "NZ", types: Array(2)}
6: {long_name: "8061", short_name: "8061", types: Array(1)}
在文档中说
“自动完成”构造函数带有两个参数:
我尝试使用“地址”并且没有任何变化
答案 0 :(得分:0)
我遇到了同样的问题,最终只是创建了一个函数来构建所需的地址。这对美国非常有用。我不确定英国...
getAddressFromComponents(geo: any) {
const streetNumber = geo.find( g => g.types.find( t => t === 'street_number') ).long_name;
const streetName = geo.find( g => g.types.find( t => t === 'route' )).long_name;
const cityName = geo.find( g => g.types.find( t => t === 'locality') && g.types.find( t => t === 'political' )).long_name;
const stateName = geo.find( g => g.types.find( t => t === 'administrative_area_level_1') && g.types.find( t => t === 'political' ))
.long_name;
const countryName = geo.find( g => g.types.find( t => t === 'country') && g.types.find( t => t === 'political' )).long_name;
const zip = geo.find( g => g.types.find( t => t === 'postal_code' )).long_name;
return {
address: `${streetNumber} ${streetName}`,
city: cityName,
state: stateName,
country: countryName,
postalCode: zip
};
}
用法
const address = this.getAddressFromComponents( _geoResponse[ 0 ].address_components );
出于价格上的考虑,我们将改用Algolia Places,因为它更易于使用: