Maps JavaScript API'de adres doğrulamasını kullanarak bir adresi doğrulamak için aşağıdaki örnekte gösterildiği gibi, doğrulanacak adresin bulunduğu bir istek gövdesi göndererek fetchAddressValidation
yöntemini çağırın.
async function validateAddress() { // Import the Address Validation library. const {AddressValidation} = await google.maps.importLibrary('addressValidation'); // Call the fetchAddressValidation method. const result = await AddressValidation.fetchAddressValidation({ address: { postalCode: '94043', regionCode: 'US', languageCode: 'en', addressLines: ['1600 Amphitheatre', 'Parkway'], } }); // Log the results to the console. document.querySelector('pre').textContent = JSON.stringify(result, null, ' '); }
Bir adresi tek tek bileşenleri kullanarak veya biçimlendirilmiş adresin tamamını bir dizi değişmez ifadesi olarak iletmek için addressLines
kullanarak tanımlayabilirsiniz (API, adresi tek tek bileşenlere ayırır):
address: { addressLines: ['1600 Amphitheatre Parkway, Mountain View, CA 94043'], }
Sonuçları işleme
fetchAddressValidation
yöntemi, AddressValidationResponse
nesnesine çözümlenen bir söz döndürür. Bu nesne, API tarafından yapılan düzeltmeler dahil olmak üzere doğrulanmış adresi içerir. Adresin doğrulama durumunu belirlemek için yanıt nesnesinin çeşitli alanlarına erişebilirsiniz. Aşağıdaki örnekte, yanıt nesnesinin alanlarına nasıl erişileceği gösterilmektedir.
async function validateAddress() { // Import the Address Validation library. const {AddressValidation} = await google.maps.importLibrary('addressValidation'); // Call the fetchAddressValidation method. const result = await AddressValidation.fetchAddressValidation({ address: { postalCode: '94043', regionCode: 'US', languageCode: 'en', addressLines: ['1600 Amphitheatre', 'Parkway'], } }); // Log the results to the console: console.log(`Formatted address: ${result.address.formattedAddress}`); console.log(`Entered: ${result.verdict.inputGranularity}`); console.log(`Validated: ${result.verdict.validationGranularity}`); console.log(`Address complete: ${result.verdict.addressComplete}`); console.log(`Has unconfirmed components: ${result.verdict.hasUnconfirmedComponents}`); console.log(`Has inferred components: ${result.verdict.hasInferredComponents}`); console.log(`Has replaced components: ${result.verdict.hasReplacedComponents}`); }