我正在使用Google为邮政编码查询提供的简单网址。
http://maps.googleapis.com/maps/api/geocode/json?address=90210&sensor=true
此网址将返回整个世界的结果,但如何将这些限制仅限于美国?我查了一下却找不到任何东西。我不想编写超过必要的代码,以确保在可能有一种方法可以添加到URL并获得以下内容时没有任何美国地址:
{
"results" : [],
"status" : "ZERO_RESULTS"
}
如果根本没有结果,这就是API返回的内容。更容易。
答案 0 :(得分:4)
您可以使用component filtering并使用country
和postal_code
组件
http://maps.googleapis.com/maps/api/geocode/json?sensor=true&components=country:US|postal_code:90210
答案 1 :(得分:0)
当您尝试在问题中提供的链接时,Google可能会出现故障。我只是尝试过它并且工作正常:
{
"results" : [
{
"address_components" : [
{
"long_name" : "90210",
"short_name" : "90210",
"types" : [ "postal_code" ]
},
{
"long_name" : "Beverly Hills",
"short_name" : "Beverly Hills",
"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" ]
}
],
"formatted_address" : "Beverly Hills, CA 90210, USA",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 34.1354771,
"lng" : -118.3867129
},
"southwest" : {
"lat" : 34.065094,
"lng" : -118.4423781
}
},
"location" : {
"lat" : 34.1030032,
"lng" : -118.4104684
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 34.1354771,
"lng" : -118.3867129
},
"southwest" : {
"lat" : 34.065094,
"lng" : -118.4423781
}
}
},
"types" : [ "postal_code" ]
}
],
"status" : "OK"
}