使用Google API匹配城市和州?

时间:2014-08-25 11:04:15

标签: javascript jquery google-maps google-maps-api-3

我需要使用Google API检查城市和州的特定组合是否正确。

到目前为止找到的方式

1。使用GeoCoding API

我们可以使用zipCode进行调用,它将返回一个JSON。有一个描述格式化地址的字段,其中包含城市,州,邮政编码,区域等。

http://maps.googleapis.com/maps/api/geocode/json?address=ZIPCODE&sensor=true

2。 ZIPtastic API

与上述类似,但会返回少量信息。

但现在我想没有ZIPCode。 [即]给出了城市和州,我需要使用API​​检查给定的组合是否正确。有什么方法吗?请提出一些想法。

编辑:     我需要检查给定的城市,州,国家,邮政编码是否正确。

http://maps.googleapis.com/maps/api/geocode/json?address=NY&address=10001&sensor=true&components=country:US

上述内容返回与

相同的结果
http://maps.googleapis.com/maps/api/geocode/json?address=NY&address=10001&sensor=true&address=albanycomponents=country:US

Zipcode是10001,State是NY,City是Albany,Country是US。为什么它会返回相同的结果。我得到了正确的结果还是做了什么?

1 个答案:

答案 0 :(得分:2)

您可以在GeoCoding API的address参数中使用您想要的内容,例如:

http://maps.googleapis.com/maps/api/geocode/json?address=Paris&sensor=true

我在这里得到的结果与地址Paris匹配:法国巴黎,德克萨斯州巴黎等。

因此,您可以使用它输入您的城市并检查状态是否有效。为此,请使用以下补码限制您的搜索到美国:components=country:US

示例:http://maps.googleapis.com/maps/api/geocode/json?address=Paris&sensor=true&components=country:US

结果:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Paris",
               "short_name" : "Paris",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Comté de Lamar",
               "short_name" : "Comté de Lamar",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Texas",
               "short_name" : "TX",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "États-Unis",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Paris, Texas, États-Unis",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 33.7383781,
                  "lng" : -95.435455
               },
               "southwest" : {
                  "lat" : 33.6118529,
                  "lng" : -95.62792789999999
               }
            },
            "location" : {
               "lat" : 33.6609389,
               "lng" : -95.55551299999999
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 33.7383781,
                  "lng" : -95.435455
               },
               "southwest" : {
                  "lat" : 33.6118529,
                  "lng" : -95.62792789999999
               }
            }
         },
         "types" : [ "locality", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Paris",
               "short_name" : "Paris",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Comté de Henry",
               "short_name" : "Comté de Henry",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Tennessee",
               "short_name" : "TN",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "États-Unis",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "38242",
               "short_name" : "38242",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Paris, Tennessee 38242, États-Unis",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 36.3291321,
                  "lng" : -88.2650759
               },
               "southwest" : {
                  "lat" : 36.266,
                  "lng" : -88.36711489999999
               }
            },
            "location" : {
               "lat" : 36.3020023,
               "lng" : -88.32671069999999
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 36.3291321,
                  "lng" : -88.2650759
               },
               "southwest" : {
                  "lat" : 36.266,
                  "lng" : -88.36711489999999
               }
            }
         },
         "types" : [ "locality", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Paris",
               "short_name" : "Paris",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Paris",
               "short_name" : "Paris",
               "types" : [ "administrative_area_level_3", "political" ]
            },
            {
               "long_name" : "Comté d'Edgar",
               "short_name" : "Comté d'Edgar",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Illinois",
               "short_name" : "IL",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "États-Unis",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "61944",
               "short_name" : "61944",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Paris, Illinois 61944, États-Unis",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 39.6485756,
                  "lng" : -87.6505408
               },
               "southwest" : {
                  "lat" : 39.581415,
                  "lng" : -87.721046
               }
            },
            "location" : {
               "lat" : 39.611146,
               "lng" : -87.6961374
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 39.6485756,
                  "lng" : -87.6505408
               },
               "southwest" : {
                  "lat" : 39.581415,
                  "lng" : -87.721046
               }
            }
         },
         "types" : [ "locality", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Paris",
               "short_name" : "Paris",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Comté de Bourbon",
               "short_name" : "Comté de Bourbon",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Kentucky",
               "short_name" : "KY",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "États-Unis",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "40361",
               "short_name" : "40361",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Paris, Kentucky 40361, États-Unis",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 38.238271,
                  "lng" : -84.232089
               },
               "southwest" : {
                  "lat" : 38.164922,
                  "lng" : -84.3073259
               }
            },
            "location" : {
               "lat" : 38.2097987,
               "lng" : -84.2529869
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 38.238271,
                  "lng" : -84.232089
               },
               "southwest" : {
                  "lat" : 38.164922,
                  "lng" : -84.3073259
               }
            }
         },
         "types" : [ "locality", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Paris",
               "short_name" : "Paris",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Comté d'Oxford",
               "short_name" : "Comté d'Oxford",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Maine",
               "short_name" : "ME",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "États-Unis",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Paris, Maine, États-Unis",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 44.31228309999999,
                  "lng" : -70.4148551
               },
               "southwest" : {
                  "lat" : 44.1753699,
                  "lng" : -70.56617199999999
               }
            },
            "location" : {
               "lat" : 44.2597917,
               "lng" : -70.5006152
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 44.31228309999999,
                  "lng" : -70.4148551
               },
               "southwest" : {
                  "lat" : 44.1753699,
                  "lng" : -70.56617199999999
               }
            }
         },
         "types" : [ "locality", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Paris",
               "short_name" : "Paris",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Short Mountain",
               "short_name" : "Short Mountain",
               "types" : [ "administrative_area_level_3", "political" ]
            },
            {
               "long_name" : "Comté de Logan",
               "short_name" : "Comté de Logan",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Arkansas",
               "short_name" : "AR",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "États-Unis",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "72855",
               "short_name" : "72855",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Paris, Arkansas 72855, États-Unis",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 35.3064849,
                  "lng" : -93.67508309999999
               },
               "southwest" : {
                  "lat" : 35.2672499,
                  "lng" : -93.7618069
               }
            },
            "location" : {
               "lat" : 35.2920325,
               "lng" : -93.7299173
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 35.3064849,
                  "lng" : -93.67508309999999
               },
               "southwest" : {
                  "lat" : 35.2672499,
                  "lng" : -93.7618069
               }
            }
         },
         "types" : [ "locality", "political" ]
      }
   ],
   "status" : "OK"
}

然后你解析JSON并且能够找到你想要的东西,这里有administrative_area_level_1字段用于状态!

修改:此外,您还可以指定您的搜索仅在locality字段上:

http://maps.googleapis.com/maps/api/geocode/json?sensor=true&components=country:US|locality:Paris过滤来自其他文件中名称Paris的可能结果