javascript - 使用 Google API 匹配城市和州?

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

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

到目前为止我发现的方法

<强>1。使用地理编码 API:

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

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

<强>2。 ZIPtastic API

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

但现在我不想使用邮政编码。 [即] 给出了城市和州,我需要使用 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

邮政编码为 10001,州为 NY,城市为奥尔巴尼,国家/地区为 US。为什么它返回相同的结果。我得到了正确的结果或做了什么吗?

最佳答案

您可以在 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 用于过滤来自名称 的可能结果巴黎在其他领域

关于javascript - 使用 Google API 匹配城市和州?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25484488/

相关文章:

安卓 : which offline maps use in app

android - 通过安卓应用分享谷歌地图静态图片

javascript - 单击函数 javascript 和 zindex 时出错

jquery - 使输入文本与 Bootstrap 选择器的高度相同

javascript - 按钮单击事件在页面重新加载时触发

javascript - 计算复选框总数

javascript - 如何获取超链接的 ID?

java - 如何将csv文件中的数据解析到容器中并搜索1个值并在java中检索其行上的其余值

javascript - 匹配日期时无法理解 Javascript 中的正则表达式

javascript - 如何去除javascript中的不透明度?