【发布时间】:2020-03-28 08:31:45
【问题描述】:
我需要返回存在于邮政编码周围的任何(至少一个)有效地址。 国家永远都是一样的。
我怎样才能做到这一点?
谢谢,
【问题讨论】:
我需要返回存在于邮政编码周围的任何(至少一个)有效地址。 国家永远都是一样的。
我怎样才能做到这一点?
谢谢,
【问题讨论】:
您可以通过 2 个请求来完成此操作,将第一个请求的输出用作第二个请求的输入。最后列出了一些注意事项。
请求 1
根据邮政编码获取数据:
curl --location --request GET 'https://maps.googleapis.com/maps/api/geocode/json?address=90058&key={{YOUR_API_KEY}}'
从您的回复中,您将想要获取results[0].geometry.location 对象。您将在下一个查询中使用 lat 和 lng 值。上面的示例提供了以下内容:
// … more above
"formatted_address" : "Los Angeles, CA 90058, USA",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 34.045639,
"lng" : -118.1685449
},
"southwest" : {
"lat" : 33.979672,
"lng" : -118.2435201
}
},
"location" : {
"lat" : 34.00637469999999,
"lng" : -118.2234229
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 34.045639,
"lng" : -118.1685449
},
"southwest" : {
"lat" : 33.979672,
"lng" : -118.2435201
}
}
},
"place_id" : "ChIJDeH8s8TIwoARYQFWkBcCzFk",
// … more below
请求 2
根据经纬度获取数据
curl --location --request GET 'https://maps.googleapis.com/maps/api/geocode/json?latlng=34.00637469999999,-118.2234229&key={{YOUR_API_KEY}}'
注意!查询参数已从address 更改为latlng
这可能会返回许多结果,您必须决定哪种类型符合您的要求,但我建议循环遍历结果数组并查找包含 street_address 或 premise 的 types 数组的条目并将该条目用作您的目标结果集。
对于上面的示例,results[1] 将具有 "types" : [ "premise" ]
其关联的formatted_address 和完整的父对象如下:
"formatted_address" : "2727 E Vernon Ave, Vernon, CA 90058, USA",
// … more above
{
"address_components": [
{
"long_name": "2727",
"short_name": "2727",
"types": [
"street_number"
]
},
{
"long_name": "East Vernon Avenue",
"short_name": "E Vernon Ave",
"types": [
"route"
]
},
{
"long_name": "Vernon",
"short_name": "Vernon",
"types": [
"locality",
"political"
]
},
{
"long_name": "Los Angeles County",
"short_name": "Los Angeles County",
"types": [
"administrative_area_level_2",
"political"
]
},
{
"long_name": "California",
"short_name": "CA",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "United States",
"short_name": "US",
"types": [
"country",
"political"
]
},
{
"long_name": "90058",
"short_name": "90058",
"types": [
"postal_code"
]
},
{
"long_name": "1822",
"short_name": "1822",
"types": [
"postal_code_suffix"
]
}
],
"formatted_address": "2727 E Vernon Ave, Vernon, CA 90058, USA",
"geometry": {
"bounds": {
"northeast": {
"lat": 34.0065676,
"lng": -118.2228652
},
"southwest": {
"lat": 34.0057081,
"lng": -118.2245065
}
},
"location": {
"lat": 34.0061691,
"lng": -118.2236056
},
"location_type": "ROOFTOP",
"viewport": {
"northeast": {
"lat": 34.0074868302915,
"lng": -118.2223368697085
},
"southwest": {
"lat": 34.0047888697085,
"lng": -118.2250348302915
}
}
},
"place_id": "ChIJCQv2_sPIwoARZn8W2bF9a9g",
"types": [
"premise"
]
},
// … more below
注意事项
route 或street_address 但没有premise 的结果。我在“加利福尼亚州弗农 (90058) - 人口 112”和“堪萨斯州弗里波特城 (67049) - 人口 5”上对此进行了测试。弗里波特城没有返回 premise 类型的结果。&result_type=premise 应用于请求 2。但是,如果给定 lat/lng 不存在任何结果使用您指定的类型,您将收到 results.status = ZERO_RESULTS。result_type 值,用管道分隔 (|)【讨论】: