I am trying to get autocomplete list based on input with google maps api in php. I want to get only results from Europe. I tried using ‘components’ => ‘country:de’ filter but that gets only one to five countries and I need more. Any help is appreciated. Here is my code.
php class
<?php
namespace AppSupport;
use GuzzleHttpClient;
use IlluminateSupportFacadesDB;
use SymfonyComponentHttpFoundationJsonResponse;
use SymfonyComponentHttpFoundationResponse;
class AutocompleteHandler
{
public const BASE_URL = "https://maps.googleapis.com/maps/api/place";
private $key;
public function __construct()
{
$this->key = config('services.googlekey.key');
}
public function placeId(string $address): JsonResponse
{
$url = sprintf(
'%s/autocomplete/json?%s',
self::BASE_URL,
http_build_query([
'input' => $address,
'types' => '(cities)',
'components' => 'country:de',
'key' => $this->key,
]));
try {
$client = new Client();
$response = $client->request('get', $url);
$responseJson = $response->getBody()->getContents();
$responseArray = json_decode($responseJson, true);
return response()->json(collect($responseArray['predictions'])->map(
function ($value) {
return [
'id' => $value['place_id'],
'label' => $value['description'],
];
}
));
} catch (Exception $e) {
return response()->json([
'error' => $e->getMessage(),
], Response::HTTP_INTERNAL_SERVER_ERROR);
}
}
}