如何限制Google Maps地理编码器?

问题描述

我有一个网页,允许用户在输入中键入位置并找到我公司的哪些物理位置最近。大多数情况下都有效。我们在所有营业地点都有LatLng;我们将用户的输入发送到Google Geocoding API,并从那里获取LatLng来找到最接近的。

我正在尝试解决用户不太明确时出现的问题。我们所有的地点都在德克萨斯州,因此,如果用户在输入中输入“ paris”,我希望Geocoding API可以返回德克萨斯州巴黎的搜索结果。不幸的是,该输入仅返回一个结果,而不是在埃菲尔铁塔上戴着牛仔帽的结果。

我已经阅读过有关视口偏移和区域偏移的内容;它们看起来像我所需要的,但是却没有按我期望的那样工作。 https://maps.googleapis.com/maps/api/geocode/json?address=paris&key=MY_KEY&region=ushttps://maps.googleapis.com/maps/api/geocode/json?address=paris&key=MY_KEY&bounds=25.837377,-106.645646|36.500704,-93.508292(以及两个参数的组合)仅返回法国巴黎。有人可以建议吗?

解决方法

您可以使用&components=country:US

将地址解析器限制在美国

请参阅文档中的component filtering

https://maps.googleapis.com/maps/api/geocode/json?address=paris&components=country:US&key=MY_KEY

返回:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Lamar County","short_name" : "Lamar County","types" : [ "administrative_area_level_2","political" ]
            },{
               "long_name" : "Texas","short_name" : "TX","types" : [ "administrative_area_level_1",{
               "long_name" : "United States","short_name" : "US","types" : [ "country","political" ]
            }
         ],"formatted_address" : "Lamar County,TX,USA","geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 33.9430568,"lng" : -95.3080719
               },"southwest" : {
                  "lat" : 33.3776528,"lng" : -95.857738
               }
            },"location" : {
               "lat" : 33.75694,"lng" : -95.6457951
            },"location_type" : "APPROXIMATE","viewport" : {
               "northeast" : {
                  "lat" : 33.9430568,"lng" : -95.857738
               }
            }
         },"place_id" : "ChIJf6k5FFj_SoYRwuSdUP31HL0","political" ]
      },{
         "address_components" : [
            {
               "long_name" : "United States","formatted_address" : "United States","geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 71.5388001,"lng" : -66.885417
               },"southwest" : {
                  "lat" : 18.7763,"lng" : 170.5957
               }
            },"location" : {
               "lat" : 37.09024,"lng" : -95.712891
            },"viewport" : {
               "northeast" : {
                  "lat" : 71.5388001,"lng" : 170.5957
               }
            }
         },"place_id" : "ChIJCzYy5IS16lQRQrfeQ5K5Oxw",{
         "address_components" : [
            {
               "long_name" : "156","short_name" : "156","types" : [ "street_number" ]
            },{
               "long_name" : "Paris Street","short_name" : "Paris St","types" : [ "route" ]
            },{
               "long_name" : "Kansas City","short_name" : "KCMO","types" : [ "locality",{
               "long_name" : "Carroll Township","short_name" : "Carroll Township","types" : [ "administrative_area_level_3",{
               "long_name" : "Platte County","short_name" : "Platte County",{
               "long_name" : "Missouri","short_name" : "MO",{
               "long_name" : "64153","short_name" : "64153","types" : [ "postal_code" ]
            }
         ],"formatted_address" : "156 Paris St,Kansas City,MO 64153,"geometry" : {
            "location" : {
               "lat" : 39.3044129,"lng" : -94.7207093
            },"location_type" : "ROOFTOP","viewport" : {
               "northeast" : {
                  "lat" : 39.30576188029149,"lng" : -94.71936031970849
               },"southwest" : {
                  "lat" : 39.30306391970849,"lng" : -94.72205828029151
               }
            }
         },"place_id" : "ChIJg_6e7QZewIcRDuO25dr6ddg","plus_code" : {
            "compound_code" : "873H+QP Kansas City,MO,"global_code" : "86F7873H+QP"
         },"types" : [ "establishment","finance","point_of_interest","post_office" ]
      },{
         "address_components" : [
            {
               "long_name" : "Paris","short_name" : "Paris",{
               "long_name" : "Lamar County","formatted_address" : "Paris,"geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 33.7383781,"lng" : -95.435455
               },"southwest" : {
                  "lat" : 33.6118529,"lng" : -95.62792789999999
               }
            },"location" : {
               "lat" : 33.6609389,"lng" : -95.55551299999999
            },"viewport" : {
               "northeast" : {
                  "lat" : 33.7383781,"lng" : -95.62792789999999
               }
            }
         },"place_id" : "ChIJmysnFgZYSoYRSfPTL2YJuck",{
         "address_components" : [
            {
               "long_name" : "Edgar County","short_name" : "Edgar County",{
               "long_name" : "Illinois","short_name" : "IL","formatted_address" : "Edgar County,IL,"geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 39.88299200000001,"lng" : -87.53164
               },"southwest" : {
                  "lat" : 39.476646,"lng" : -87.96880899999999
               }
            },"location" : {
               "lat" : 39.71312229999999,"lng" : -87.77633329999999
            },"viewport" : {
               "northeast" : {
                  "lat" : 39.88299200000001,"lng" : -87.96880899999999
               }
            }
         },"place_id" : "ChIJkST6Ms69cogRCAPGWoBqOUQ",{
               "long_name" : "Lane County","short_name" : "Lane County",{
               "long_name" : "Oregon","short_name" : "OR",{
               "long_name" : "97390","short_name" : "97390",OR 97390,"geometry" : {
            "location" : {
               "lat" : 44.2526208,"lng" : -123.7706633
            },"viewport" : {
               "northeast" : {
                  "lat" : 44.2606124,"lng" : -123.7546559
               },"southwest" : {
                  "lat" : 44.2446281,"lng" : -123.7866707
               }
            }
         },"place_id" : "ChIJleXtq1KjwVQRLiSkFazeFHU",{
         "address_components" : [
            {
               "long_name" : "Jordan","short_name" : "Jordan","types" : [ "colloquial_area",{
               "long_name" : "Washington Township","short_name" : "Washington Township",{
               "long_name" : "Daviess County","short_name" : "Daviess County",{
               "long_name" : "Indiana","short_name" : "IN",{
               "long_name" : "47501","short_name" : "47501","formatted_address" : "Jordan,Washington Township,IN 47501,"geometry" : {
            "location" : {
               "lat" : 38.6886591,"lng" : -87.206957
            },"viewport" : {
               "northeast" : {
                  "lat" : 38.697368,"lng" : -87.1909496
               },"southwest" : {
                  "lat" : 38.6799492,"lng" : -87.2229644
               }
            }
         },"place_id" : "ChIJ0e364hPebYgRigmh1xFd6TI",{
         "address_components" : [
            {
               "long_name" : "Palestine","short_name" : "Palestine","types" : [ "neighborhood",{
               "long_name" : "Paris",{
               "long_name" : "Henry County","short_name" : "Henry County",{
               "long_name" : "Tennessee","short_name" : "TN",{
               "long_name" : "38242","short_name" : "38242","formatted_address" : "Palestine,Paris,TN 38242,"geometry" : {
            "location" : {
               "lat" : 36.2589479,"lng" : -88.40199149999999
            },"viewport" : {
               "northeast" : {
                  "lat" : 36.2679447,"lng" : -88.3859841
               },"southwest" : {
                  "lat" : 36.2499501,"lng" : -88.4179989
               }
            }
         },"place_id" : "ChIJ4wFtXAe-e4gRs1UnOjUUm1c",{
         "address_components" : [
            {
               "long_name" : "Texas","formatted_address" : "Texas,"geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 36.5007041,"lng" : -93.5080389
               },"southwest" : {
                  "lat" : 25.8371638,"lng" : -106.6456461
               }
            },"location" : {
               "lat" : 31.9685988,"lng" : -99.9018131
            },"viewport" : {
               "northeast" : {
                  "lat" : 36.5007041,"lng" : -106.6456461
               }
            }
         },"place_id" : "ChIJSTKCCzZwQIYRPN4IGI8c6xY",{
         "address_components" : [
            {
               "long_name" : "Monroe County","short_name" : "Monroe County","formatted_address" : "Monroe County,"geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 39.662932,"lng" : -91.711934
               },"southwest" : {
                  "lat" : 39.3373651,"lng" : -92.31004899999999
               }
            },"location" : {
               "lat" : 39.5508815,"lng" : -92.0665197
            },"viewport" : {
               "northeast" : {
                  "lat" : 39.662932,"lng" : -92.31004899999999
               }
            }
         },"place_id" : "ChIJOV2qxv0M3YcRAKucv30D33c","political" ]
      }
   ],"status" : "OK"
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...