Kullanıcının bulunduğu yere girdiği girdim var. Bu yerden şehir, eyalet ve posta almak ve bu bilgileri başka bir girişe (şehir, eyalet ve posta girişleri) girmek istiyorum, bu yüzden kullanıcının bu bilgileri elle doldurması gerekmiyor.Google Otomatik Tamamlama - yüklü adresle ilgili bilgileri nasıl alırsınız?
here sonucunu başarmaya çalıştığım bir şeyi buldum ve bunları ihtiyaçlarıma göre değiştirmeye çalışıyorum.
Yani işte gidiyorum:
var placeSearch, autocomplete_origin_address, autocomplete_destination_address;
var componentForm_origin = {
user_origin_city: 'user_origin_city',
user_origin_state: 'user_origin_state',
user_origin_zip: 'user_origin_zip'
};
function initAutocomplete() {
console.log('in "initAutocomplete"');
autocomplete_origin_address = new google.maps.places.Autocomplete(
document.getElementById('user_origin_address'),
types: ['geocode']}
);
autocomplete_origin_address.addListener("place_changed",
fillInAddress(autocomplete_origin_address,
"user_origin_address",
"user_origin_city",
"user_origin_state",
"user_origin_zip"));
}
function fillInAddress(autocomplete_resource, address, city, state, zip) {
console.log("! !x");
// Get the place details from the autocomplete object.
var place = autocomplete_resource.getPlace();
// origin fields to populate with the selected address
var fetched_street_number = fetchAddressDetails(place, "street_number", "long_name");
var fetched_address = fetchAddressDetails(place, "route", "long_name");
$("#"+address).val(fetched_street_number + " " + address);
var fetched_city = fetchAddressDetails(place, "locality", "long_name");
$("#"+city).val(fetched_city);
var fetched_state = fetchAddressDetails(place, "administrative_area_level_1", "short_name");
$("#"+state).val(fetched_state);
var fetched_zip = fetchAddressDetails(place, "postal_code", "long_name");
$("#"+zip).val(fetched_zip);
}
function fetchAddressDetails(autocomplete_place, detail_type, name_type){
console.log("in fetchAddressDetails");
var item = $.grep(autocomplete_place.address_components,
function(e){ return e.types[0] == detail_type; });
var item_data = item[0][name_type];
console.log("item_data: "+item_data);
return item_data;
}
initAutocomplete();
Ama sayfasını yüklediğinizde, bu hatayı alıyorum:
in "initAutocomplete"
! !x
in fetchAddressDetails
Uncaught TypeError: Cannot read property 'address_components' of undefined
Sorunu google çalışıyorum, ancak ondan kurtulmak edemez ve Kod çalışması yap. Genelde denediğim şey - Kullanıcıların adresi girdiği sayfada 4 girdim var. Bu yüzden - bu 4 girişin her birine bir adres girdiğinde - Google otomatik tamamlama işlevini yakıp söndürün, adresi getirin ve şehir/durum/posta bilgilerini ilgili girişlere getirin.
Neden şu anda bir şey almıyorsunuz?
Önceden teşekkür ederiz.
veriniz ... Bir [Minimal, Tam soruna neden olabilecek bir sözdizimi hatası sadece bir yazım hatası var Gerekli HTML/CSS dahil olmak üzere, sorununuzu gösteren Test Edilmiş ve Okunabilir örnek] (http://stackoverflow.com/help/mcve). – geocodezip