验证地址
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
如需在 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}`);
}
后续步骤
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-31。
[null,null,["最后更新时间 (UTC):2025-08-31。"],[],[],null,["| This product or feature is in Preview (pre-GA). Pre-GA products and features might have limited support, and changes to pre-GA products and features might not be compatible with other pre-GA versions. Pre-GA Offerings are covered by the [Google\n| Maps Platform Service Specific Terms](https://cloud.google.com/maps-platform/terms/maps-service-terms). For more information, see the [launch stage\n| descriptions](/maps/launch-stages).\n\n\nTo validate an address using Address Validation in Maps JavaScript API, call the `fetchAddressValidation`\nmethod passing a request body with the address to validate, as shown in the following example. \n\n```javascript\nasync function validateAddress() {\n // Import the Address Validation library.\n const {AddressValidation} =\n await google.maps.importLibrary('addressValidation');\n // Call the fetchAddressValidation method.\n const result = await AddressValidation.fetchAddressValidation({\n address: {\n postalCode: '94043',\n regionCode: 'US',\n languageCode: 'en',\n addressLines: ['1600 Amphitheatre', 'Parkway'],\n }\n });\n // Log the results to the console.\n document.querySelector('pre').textContent =\n JSON.stringify(result, null, ' ');\n}\n \n```\n\nYou can define an address by using individual components, or by using `addressLines`\nto pass the entire formatted address as an array literal (the API will parse the address\ninto individual components): \n\n```javascript\naddress: {\n addressLines: ['1600 Amphitheatre Parkway, Mountain View, CA 94043'],\n}\n \n```\n\nHandle the results\n\nThe `fetchAddressValidation` method returns a promise that resolves to an\n`AddressValidationResponse` object. This object contains the validated address,\nincluding any corrections made by the API. You can access the various fields of the\nresponse object to determine the validation status of the address. The following example\nshows how to access the fields of the response object. \n\n```javascript\nasync function validateAddress() {\n // Import the Address Validation library.\n const {AddressValidation} =\n await google.maps.importLibrary('addressValidation');\n // Call the fetchAddressValidation method.\n const result = await AddressValidation.fetchAddressValidation({\n address: {\n postalCode: '94043',\n regionCode: 'US',\n languageCode: 'en',\n addressLines: ['1600 Amphitheatre', 'Parkway'],\n }\n });\n // Log the results to the console:\n console.log(`Formatted address: ${result.address.formattedAddress}`);\n console.log(`Entered: ${result.verdict.inputGranularity}`);\n console.log(`Validated: ${result.verdict.validationGranularity}`);\n console.log(`Address complete: ${result.verdict.addressComplete}`);\n console.log(`Has unconfirmed components: ${result.verdict.hasUnconfirmedComponents}`);\n console.log(`Has inferred components: ${result.verdict.hasInferredComponents}`);\n console.log(`Has replaced components: ${result.verdict.hasReplacedComponents}`);\n}\n \n```\n\nNext steps\n\n- [Understand a basic address validation response](/maps/documentation/javascript/address-validation/understand-response)"]]