Я использую Google Maps v3 API для автозаполнения формы адреса, но я не хочу использовать общее поле поиска, как в примере (https://google-developers.appspot.com/maps/documentation/javascript/examples/full/places-autocomplete-addressform) .
Я пытаюсь сделать поле адреса улицы полем поиска, а также только поле номера улицы и названия улицы. Я нашел этот пример на скрипте JS (https://jsfiddle.net/smartystreets/0c9fy73v/) Я не могу это воспроизвести.
Я попытался отключить autocomplete=off, но не работает в chrome (для elementId('autocomplete'))
Я попытался скрыть элементы ввода маршрута и улицы и установить пустое значение идентификатора ввода.
document.getElementById('autocomplete').value = '';
Затем, используя скрытые поля ввода, перестроить эту форму ввода с
document.getElementById('autocomplete').value = document.getElementById('street_number') + ' ' + document.getElementById('route');
Любые идеи, что я делаю неправильно? Почему поле поиска не очищается?
Это мой HTML для моего поля поиска/адреса:
<!-- address -->
<div class="col-md-6 col-lg-6">
<label class="light text-capitalize">address:</label>
<input type="text" id="autocomplete"
name="street" placeholder="Street address, PO Box, etc.">
<input type="hidden" id="route" disabled="false">
<input type="hidden" id="street_number" disabled="false">
<input type="text" name="address1" placeholder="(Optional) Apartment, Suite, Floor, etc.">
</div>
<!-- address end -->
Это мой встроенный JS
var placeSearch, autocomplete;
var componentForm = {
street_number: 'short_name',
route: 'long_name',
locality: 'long_name',
administrative_area_level_1: 'short_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);
}
function fillInAddress() {
// Get the place details from the autocomplete object.
var place = autocomplete.getPlace();
for (var component in componentForm) {
document.getElementById('street_number').value = '';
document.getElementById('route').value = '';
document.getElementById('locality').value = '';
document.getElementById('administrative_area_level_1').value = '';
// document.getElementById('country').value = '';
document.getElementById('postal_code').value = '';
}
// 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>