NovinkaZverejnili sme 50+ Laravel balíkov ako open source
Custom AI apps, agents and automation — Roundly ConsultingRoundly
Všetky balíky
Google Places for Laravel

Našepkávanie (autocomplete)

autocomplete() vracia kolekciu AutocompletePrediction — návrhy, aké poznáte z vyhľadávacieho poľa Google Maps. Odovzdajte reťazec alebo AutocompleteQuery s voľbami:

use RoundlyConsulting\GooglePlaces\DataTransferObjects\AutocompletePrediction;
use RoundlyConsulting\GooglePlaces\DataTransferObjects\AutocompleteQuery;
use RoundlyConsulting\GooglePlaces\DataTransferObjects\Location;
use RoundlyConsulting\GooglePlaces\DataTransferObjects\LocationDefinition;
use RoundlyConsulting\GooglePlaces\Facades\GooglePlaces;

// String shorthand:
$predictions = GooglePlaces::autocomplete('Luxury Restaurant');

/** @var AutocompletePrediction $prediction */
$prediction = $predictions->first();

echo $prediction->description;
echo $prediction->placeId;
echo $prediction->mainText;       // structured label (nullable)
echo $prediction->secondaryText;  // structured label (nullable)
echo implode(', ', $prediction->types);

// Full query (immutable, fluent):
$query = (new AutocompleteQuery('Coffee'))
    ->ofType('cafe', 'restaurant')               // max 5 included primary types
    ->preferInArea((new LocationDefinition())->circle(new Location(48.1486, 17.1077), radius: 2000))
    ->inRegions('sk')                            // included region codes
    ->fromOrigin(new Location(48.1486, 17.1077))
    ->usingSessionToken('a-session-token');

$predictions = GooglePlaces::autocomplete($query);

Voľby dopytu

MetódaÚčinok
withInput(string $input)Nahradí vstupný text.
inLanguage(string $language)Jazyk odpovede (predvolene en).
ofType(string ...$types)Zahrnuté primárne typy — max. 5, inak PlacesException.
inRegions(string ...$codes)Zahrnuté kódy regiónov — max. 15.
preferInArea(LocationDefinition $area)Preferovaná oblasť — výsledky v jej blízkosti sa radia vyššie.
restrictTo(LocationDefinition $area)Obmedzenie oblasti — len výsledky vnútri oblasti.
nearby(float $lat, float $lng, int $radius = 5000)Skratka pre preferovaný kruh.
withinBounds(Location $low, Location $high)Skratka pre obmedzenie na obdĺžnik.
fromOrigin(?Location $origin)Východiskový bod odoslaný s požiadavkou.
usingSessionToken(string $token)Pripojí token billing session (session() to urobí za vás).

Preferovaná alebo obmedzená oblasť

Preferovaná oblasť (bias) uprednostní výsledky v jej blízkosti; obmedzenie (restriction) pripustí len výsledky v nej. Použite skratky alebo si kruh či obdĺžnik LocationDefinition zostavte sami:

use RoundlyConsulting\GooglePlaces\DataTransferObjects\AutocompleteQuery;
use RoundlyConsulting\GooglePlaces\DataTransferObjects\Location;
use RoundlyConsulting\GooglePlaces\DataTransferObjects\LocationDefinition;

// Bias toward a circle (radius in metres, default 5000):
(new AutocompleteQuery('Coffee'))->nearby(48.1486, 17.1077, radius: 2000);

// Restrict to a rectangle between two corners:
(new AutocompleteQuery('Coffee'))->withinBounds(new Location(48.1, 17.0), new Location(48.2, 17.2));

// Or build the area yourself and pass it as a bias or a restriction:
$area = (new LocationDefinition())->circle(new Location(48.1486, 17.1077), radius: 2000);

(new AutocompleteQuery('Coffee'))->preferInArea($area);
(new AutocompleteQuery('Coffee'))->restrictTo($area);

Billing session

Spojte sériu našepkávaní a záverečné volanie details() do jednej fakturačnej session Google. Session automaticky používa jeden token a po vyriešení details() sa uzavrie (ďalšie volanie vyhodí PlacesException):

$session = GooglePlaces::session();

$session->autocomplete('piz');
$session->autocomplete('pizza ne');   // same session token
$place = $session->details($placeId); // billed as one session, then closed

Token je predvolene nové UUID; odovzdaním vlastného tokenu môžete session obnoviť naprieč požiadavkami (napríklad jeden token na jedno vyplnenie formulára):

$session = GooglePlaces::session();            // token defaults to a fresh UUID
$session = GooglePlaces::session($token);      // or resume with your own token

$session->token();       // the token sent to Google
$session->isFinished();  // true once details() has resolved

$session->autocomplete((new AutocompleteQuery('pizza'))->inLanguage('sk')); // query objects work too

Našepkávanie sa nikdy necachuje — každý odoslaný vstup je živá požiadavka, preto vstup na strane klienta obmedzte (debounce).

Prejavte lásku k open source

Tento balík je zadarmo pod licenciou MIT. Ak vám šetrí čas, jednorazový príspevok alebo členstvo na Patreone nám pomôže ho ďalej udržiavať, testovať a dokumentovať.

Ďalšie spôsoby podpory vrátane kryptomien

Odoslaním daru súhlasíte s našimi podmienkami prijímania darov.

Chcete to zabudovať do svojho produktu?

Naše balíky integrujeme do zákazkových Laravel a AI riešení. Napíšte nám, na čom pracujete, a ozveme sa do 48 hodín.