Я пытаюсь получить полный адрес от автозаполнения карт 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, Нью-Брайтон, Крайстчёрч, Новая Зеландия", и это результат в моей консоли
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)}
В документации сказано:
Конструктор автозаполнения принимает два аргумента:
- Геокод указывает службе Places возвращать только результаты геокодирования, а не бизнес-результаты.
- address указывает службе Places возвращать только результаты геокодирования с точным адресом.
Я пытался использовать 'address' и ничего не менялось
- Любое представление о том, какузнать номер квартиры / квартиры / квартиры?