如何将 Google Places Web 服务仅限于英国?

How to restrict Google Places web service to UK only?

我正在使用 Google Places Web 服务 API 来实现我自己的自动完成功能。

https://developers.google.com/places/web-service/autocomplete

我正在使用 JS 获取我使用 PHP 发出的服务器端请求。

<?php

header('Content-Type: application/json; charset=utf-8');

$api = 'https://maps.googleapis.com/maps/api/place/autocomplete/json';
$key = 'api-key-goes-here'; // Use your own API key here.
$input = 'London'; // A hard-coded example.
$requestPath = "{$api}?&key={$key}&input={$input}";

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => $requestPath,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array(
    "cache-control: no-cache"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

$data = ($err) ? $err : $response;
echo $data;

?>

这一切都很好,我得到了正确的 JSON 响应。

但是,我从全局搜索中获得结果,理想情况下我想将其限制为仅限英国/英国。

例如,在上面的代码中,在加拿大和其他地方搜索 'London' returns a London。我怎样才能限制这个?我没有在文档中看到任何关于能够执行此操作的内容。

非常感谢您的帮助!

您应该添加 &components=country:GB 参数以将搜索限制为仅限英国。

这在文档中有描述 https://developers.google.com/places/web-service/autocomplete#place_autocomplete_requests

The country must be passed as a two character, ISO 3166-1 Alpha-2 compatible country code.