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

Matica vzdialeností

Pre mnoho štartov voči mnohým cieľom použite matrix(). Metóda spôsobu dopravy odošle požiadavku a vráti DistanceMatrix, ktorú indexujete po dvojiciach:

use RoundlyConsulting\GooglePlaces\DataTransferObjects\Location;
use RoundlyConsulting\GooglePlaces\Facades\GooglePlaces;

$matrix = GooglePlaces::matrix(
    origins: [new Location(48.1486, 17.1077), new Location(48.2082, 16.3738)],
    destinations: [new Location(50.0755, 14.4378), new Location(47.4979, 19.0402)],
)->driving();                       // ->driving() / ->walking() / ->bicycling() / ->transit()

$element = $matrix->for(0, 1);      // origin 0 → destination 1
if ($element?->hasRoute()) {
    echo $element->distance->humanReadableDistance;
    echo $element->distance->durationInSeconds;
}

$matrix->origin(0);                 // Collection<MatrixElement> keyed by destination index
$matrix->destination(1);            // Collection<MatrixElement> keyed by origin index

// Traffic-aware driving ETAs:
GooglePlaces::matrix($origins, $destinations)->departingAt(now()->addHour())->driving();

Na rozdiel od distance() matica pri nedostupnej dvojici nevyhodí výnimku — daný prvok jednoducho nemá trasu. Pred čítaním vzdialenosti overte hasRoute(). Každý MatrixElement nesie originIndex, destinationIndex, condition, distance (Distance alebo null) a stavový kód Google.

Príklad: najbližší vodič

Porovnajte všetkých vodičov so všetkými objednávkami jednou požiadavkou a vyberte najrýchlejšieho vodiča pre objednávku:

use RoundlyConsulting\GooglePlaces\DataTransferObjects\MatrixElement;

$matrix = GooglePlaces::matrix($drivers, $orders)->driving();

// Index of the fastest driver for order 0:
$fastest = $matrix->destination(0)
    ->filter(fn (MatrixElement $element) => $element->hasRoute())
    ->sortBy(fn (MatrixElement $element) => $element->distance->durationInSeconds)
    ->keys()
    ->first();

Priame použitie MatrixQuery

matrix() je fluentná skratka nad computeMatrix() — jej travellingBy(TravelMode) a departingAt() zodpovedajú metódam dopytu. MatrixQuery si zostavte sami, keď chcete v kóde odovzdávať celý dopyt:

use RoundlyConsulting\GooglePlaces\DataTransferObjects\MatrixQuery;
use RoundlyConsulting\GooglePlaces\Enums\TravelMode;

$matrix = GooglePlaces::computeMatrix(
    (new MatrixQuery($origins, $destinations))
        ->travellingBy(TravelMode::Bicycling)
        ->departingAt(now()->addHour()),
);

$matrix->elements;          // list<MatrixElement>, sorted by origin then destination
$matrix->originCount;       // int
$matrix->destinationCount;  // int
$matrix->type;              // TravelMode

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.