住所を確認する

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 オブジェクトに解決される Promise を返します。このオブジェクトには、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}`);
}
    

次のステップ