-1

I am using the Google Places Web Service API to roll my own autocomplete functionality.

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

I'm using JS to fetch a server-side request that I make using 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;

?>

All this works great and I get back a correct JSON response.

However, I am getting results from a global search, when Ideally I'd like to restrict it to UK / GB only.

For example, in the above code, the search for 'London' returns a London in Canada and elsewhere. How can I restrict this? I don't see anything mentioned in the documentation about being able to do this.

Many thanks for your help!

Michael Giovanni Pumo
  • 14,338
  • 18
  • 91
  • 140
  • possible duplicate: [limiting google maps autocomplete to UK address only](http://stackoverflow.com/questions/7179452/limiting-google-maps-autocomplete-to-uk-address-only) – geocodezip Dec 19 '16 at 17:13
  • Why would you down vote me? It's not a duplicate and that question was asked at least 5 years ago. I imagine it could be very outdated, which I think warrants a new discussion on this. – Michael Giovanni Pumo Dec 19 '16 at 17:15
  • @MichaelGiovanniPumo It is a duplicate, and spending five seconds Googling the term `componentRestrictions` would reveal it is not outdated. – ceejayoz Dec 19 '16 at 21:13

1 Answers1

3

You should add &components=country:GB parameter to restrict your search to UK only.

This is described in the documentation 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.

xomena
  • 31,125
  • 6
  • 88
  • 117