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

How do I restrict the Google Maps geocoder?

我有一个网页,允许用户在输入中键入位置并查找我公司的哪个物理位置最近。它的工作原理,主要是。我们的所有营业地点都有 LatLngs;我们将用户的输入发送到 Google 地理编码 API 并从那里获取 LatLng 以找到最接近的。

我正在尝试解决当用户不是很具体时出现的问题。我们所有的位置都在德克萨斯州,因此如果用户在输入中输入“巴黎”,我希望地理编码 API 到 return 德克萨斯州巴黎的结果。不幸的是,该输入只有 return 一个结果,而不是埃菲尔铁塔上戴着牛仔帽的结果。

我了解了视口偏移和区域偏移;它们看起来像我需要的,但它们没有按我预期的那样工作。两者 https://maps.googleapis.com/maps/api/geocode/json?address=paris&key=MY_KEY&region=us and https://maps.googleapis.com/maps/api/geocode/json?address=paris&key=MY_KEY&bounds=25.837377,-106.645646|36.500704,-93.508292 (以及组合两个参数) return 仅在法国巴黎。任何人都可以提出建议吗?

您可以使用 &components=country:US

将地理编码器限制在美国

参见文档component filtering

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

Returns:

{
   "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", "political" ]
            },
            {
               "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.3080719
               },
               "southwest" : {
                  "lat" : 33.3776528,
                  "lng" : -95.857738
               }
            }
         },
         "place_id" : "ChIJf6k5FFj_SoYRwuSdUP31HL0",
         "types" : [ "administrative_area_level_2", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            }
         ],
         "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
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 71.5388001,
                  "lng" : -66.885417
               },
               "southwest" : {
                  "lat" : 18.7763,
                  "lng" : 170.5957
               }
            }
         },
         "place_id" : "ChIJCzYy5IS16lQRQrfeQ5K5Oxw",
         "types" : [ "country", "political" ]
      },
      {
         "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", "political" ]
            },
            {
               "long_name" : "Carroll Township",
               "short_name" : "Carroll Township",
               "types" : [ "administrative_area_level_3", "political" ]
            },
            {
               "long_name" : "Platte County",
               "short_name" : "Platte County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Missouri",
               "short_name" : "MO",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "64153",
               "short_name" : "64153",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "156 Paris St, Kansas City, MO 64153, USA",
         "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, USA",
            "global_code" : "86F7873H+QP"
         },
         "types" : [ "establishment", "finance", "point_of_interest", "post_office" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Paris",
               "short_name" : "Paris",
               "types" : [ "locality", "political" ]
            },
            {
               "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", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Paris, TX, USA",
         "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
               }
            }
         },
         "place_id" : "ChIJmysnFgZYSoYRSfPTL2YJuck",
         "types" : [ "locality", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Edgar County",
               "short_name" : "Edgar County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Illinois",
               "short_name" : "IL",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Edgar County, IL, USA",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 39.88299200000001,
                  "lng" : -87.53164
               },
               "southwest" : {
                  "lat" : 39.476646,
                  "lng" : -87.96880899999999
               }
            },
            "location" : {
               "lat" : 39.71312229999999,
               "lng" : -87.77633329999999
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 39.88299200000001,
                  "lng" : -87.53164
               },
               "southwest" : {
                  "lat" : 39.476646,
                  "lng" : -87.96880899999999
               }
            }
         },
         "place_id" : "ChIJkST6Ms69cogRCAPGWoBqOUQ",
         "types" : [ "administrative_area_level_2", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Paris",
               "short_name" : "Paris",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Lane County",
               "short_name" : "Lane County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Oregon",
               "short_name" : "OR",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "97390",
               "short_name" : "97390",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Paris, OR 97390, USA",
         "geometry" : {
            "location" : {
               "lat" : 44.2526208,
               "lng" : -123.7706633
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 44.2606124,
                  "lng" : -123.7546559
               },
               "southwest" : {
                  "lat" : 44.2446281,
                  "lng" : -123.7866707
               }
            }
         },
         "place_id" : "ChIJleXtq1KjwVQRLiSkFazeFHU",
         "types" : [ "locality", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Jordan",
               "short_name" : "Jordan",
               "types" : [ "colloquial_area", "political" ]
            },
            {
               "long_name" : "Washington Township",
               "short_name" : "Washington Township",
               "types" : [ "administrative_area_level_3", "political" ]
            },
            {
               "long_name" : "Daviess County",
               "short_name" : "Daviess County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Indiana",
               "short_name" : "IN",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "47501",
               "short_name" : "47501",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Jordan, Washington Township, IN 47501, USA",
         "geometry" : {
            "location" : {
               "lat" : 38.6886591,
               "lng" : -87.206957
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 38.697368,
                  "lng" : -87.1909496
               },
               "southwest" : {
                  "lat" : 38.6799492,
                  "lng" : -87.2229644
               }
            }
         },
         "place_id" : "ChIJ0e364hPebYgRigmh1xFd6TI",
         "types" : [ "colloquial_area", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Palestine",
               "short_name" : "Palestine",
               "types" : [ "neighborhood", "political" ]
            },
            {
               "long_name" : "Paris",
               "short_name" : "Paris",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Henry County",
               "short_name" : "Henry County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Tennessee",
               "short_name" : "TN",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "38242",
               "short_name" : "38242",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Palestine, Paris, TN 38242, USA",
         "geometry" : {
            "location" : {
               "lat" : 36.2589479,
               "lng" : -88.40199149999999
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 36.2679447,
                  "lng" : -88.3859841
               },
               "southwest" : {
                  "lat" : 36.2499501,
                  "lng" : -88.4179989
               }
            }
         },
         "place_id" : "ChIJ4wFtXAe-e4gRs1UnOjUUm1c",
         "types" : [ "neighborhood", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Texas",
               "short_name" : "TX",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Texas, USA",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 36.5007041,
                  "lng" : -93.5080389
               },
               "southwest" : {
                  "lat" : 25.8371638,
                  "lng" : -106.6456461
               }
            },
            "location" : {
               "lat" : 31.9685988,
               "lng" : -99.9018131
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 36.5007041,
                  "lng" : -93.5080389
               },
               "southwest" : {
                  "lat" : 25.8371638,
                  "lng" : -106.6456461
               }
            }
         },
         "place_id" : "ChIJSTKCCzZwQIYRPN4IGI8c6xY",
         "types" : [ "administrative_area_level_1", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Monroe County",
               "short_name" : "Monroe County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Missouri",
               "short_name" : "MO",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Monroe County, MO, USA",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 39.662932,
                  "lng" : -91.711934
               },
               "southwest" : {
                  "lat" : 39.3373651,
                  "lng" : -92.31004899999999
               }
            },
            "location" : {
               "lat" : 39.5508815,
               "lng" : -92.0665197
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 39.662932,
                  "lng" : -91.711934
               },
               "southwest" : {
                  "lat" : 39.3373651,
                  "lng" : -92.31004899999999
               }
            }
         },
         "place_id" : "ChIJOV2qxv0M3YcRAKucv30D33c",
         "types" : [ "administrative_area_level_2", "political" ]
      }
   ],
   "status" : "OK"
}