Maps JavaScript API에서 Address Validation을 사용하여 주소를 확인하려면 다음 예와 같이 확인할 주소가 포함된 요청 본문을 전달하여 fetchAddressValidation
메서드를 호출합니다.
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, ' '); }
개별 구성요소를 사용하거나 addressLines
를 사용하여 형식이 지정된 전체 주소를 배열 리터럴로 전달하여 주소를 정의할 수 있습니다 (API가 주소를 개별 구성요소로 파싱함).
address: { addressLines: ['1600 Amphitheatre Parkway, Mountain View, CA 94043'], }
결과 처리
fetchAddressValidation
메서드는 AddressValidationResponse
객체로 확인되는 프로미스를 반환합니다. 이 객체에는 API에서 수정한 사항을 포함하여 검증된 주소가 포함됩니다. 응답 객체의 다양한 필드에 액세스하여 주소의 유효성 검사 상태를 확인할 수 있습니다. 다음 예는 응답 객체의 필드에 액세스하는 방법을 보여줍니다.
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}`); }