---
title: "Geokódovanie — Google Places for Laravel | Roundly"
description: "Preveďte súradnice na adresu a adresu na súradnice a čítajte ulicu, mesto, PSČ a krajinu cez typované metódy."
url: https://roundly-consulting.com/sk/open-source/navody/google-places-for-laravel/geokodovanie
language: sk
---

[Všetky balíky](https://roundly-consulting.com/sk/open-source.md)

[Google Places for Laravel](https://roundly-consulting.com/sk/open-source/navody/google-places-for-laravel.md)

# Geokódovanie

## Spätné geokódovanie

geocode() vracia pre súradnicu kolekciu ReverseGeocodingResult. Odpoveď ZERO\_RESULTS znamená prázdnu kolekciu, nie výnimku:

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

$results = GooglePlaces::geocode(48.1486, 17.1077);   // lat/lng shorthand
// or GooglePlaces::geocode(new Location(48.1486, 17.1077));
// or GooglePlaces::geocode(new ReverseGeocodingQuery(...));

$result = $results->first();

echo $result->address;
echo $result->placeId;

foreach ($result->components as $component) {
    echo "{$component->longName} ({$component->shortName})";
}
```

Cez ReverseGeocodingQuery filtrujete typy výsledkov a lokácií alebo si vyžiadate lokalizovanú odpoveď:

```php
use RoundlyConsulting\GooglePlaces\DataTransferObjects\Location;
use RoundlyConsulting\GooglePlaces\DataTransferObjects\ReverseGeocodingQuery;

$results = GooglePlaces::geocode(new ReverseGeocodingQuery(
    location: new Location(48.1486, 17.1077),
    resultTypes: ['street_address'],   // sent as result_type
    locationTypes: ['ROOFTOP'],        // sent as location_type
    language: 'sk',
));

$result = $results->first();
$result->geometry->location;           // Location
$result->geometry->viewportNorthEast;  // Location
$result->geometry->viewportSouthWest;  // Location
$result->types;                        // list<string>
```

## Priame geokódovanie

geocodeAddress() prevedie adresu na kolekciu výsledkov — každý so súradnicami, place id, typmi a komponentmi. GeocodingQuery pridáva inLanguage(), inRegion() a filtre komponentov cez filterBy():

```php
use RoundlyConsulting\GooglePlaces\DataTransferObjects\GeocodingQuery;
use RoundlyConsulting\GooglePlaces\Facades\GooglePlaces;

$results = GooglePlaces::geocodeAddress('1600 Amphitheatre Pkwy');

$first = $results->first();
echo $first->placeId;
echo $first->geometry->location->latitude;

// Full query with region + component filters:
$results = GooglePlaces::geocodeAddress(
    (new GeocodingQuery('Parliament'))->inRegion('sk')->filterBy('country:SK'),
);
```

## Typované komponenty adresy

Výsledky geokódovania aj miesta sprístupňujú cez components() typované metódy nad surovými komponentmi adresy:

```php
$components = GooglePlaces::geocodeAddress('1600 Amphitheatre Pkwy')->first()->components();

echo $components->streetNumber(); // "1600"
echo $components->street();       // "Amphitheatre Parkway"
echo $components->city();         // "Mountain View"
echo $components->state();        // "California"
echo $components->postalCode();   // "94043"
echo $components->country();      // "United States"
echo $components->countryCode();  // "US"

$components->has('sublocality');                                // bool
$components->first('administrative_area_level_2')?->longName;   // any component type
```

| Metóda | Typ komponentu Google |
| --- | --- |
| `streetNumber()` | street\_number |
| `street()` | route |
| `city()` | locality → postal\_town → sublocality → sublocality\_level\_1 |
| `postalCode()` | postal\_code |
| `state()` | administrative\_area\_level\_1 |
| `country()` | country (dlhý názov) |
| `countryCode()` | country (krátky názov) |

Pri objekte Place sa components() vyplní, len ak field maska obsahuje addressComponents.

## Recept: formulár s adresou

Spojte našepkávanie s priamym geokódovaním a zadanú adresu prevediete na štruktúrované polia — tu uložené cez HasAddresses z addresses-for-laravel. Ide o bežný kód hostiteľskej aplikácie; balíky nie sú navzájom prepojené:

```php
use RoundlyConsulting\GooglePlaces\Facades\GooglePlaces;

// While the user types — suggest addresses:
$predictions = GooglePlaces::autocomplete($request->input('q'));

// On submit — normalise the chosen address into structured fields:
$result = GooglePlaces::geocodeAddress($request->input('address'))->first();

$model->addAddress([
    'street'      => $result?->components()->street(),
    'city'        => $result?->components()->city(),
    'postal_code' => $result?->components()->postalCode(),
    'country'     => $result?->components()->countryCode(),
    'latitude'    => $result?->geometry->location->latitude,
    'longitude'   => $result?->geometry->location->longitude,
]);
```

## 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](https://roundly-consulting.com/sk/podporte-nas.md)

Odoslaním daru súhlasíte s našimi [podmienkami prijímania darov](https://roundly-consulting.com/sk/podmienky-darov.md).

[Podporte našu open-source prácu (otvorí sa v novej karte)](https://donate.stripe.com/dRmeVe8FX5PF1Qd9pXcEw00) [Pridajte sa k nám na Patreone (otvorí sa v novej karte)](https://www.patreon.com/cw/roundly)

## 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.

[Ponuka do 48 hodín](https://roundly-consulting.com/sk/kontakt.md) [Prezrieť všetky balíky](https://roundly-consulting.com/sk/open-source/navody/google-places-for-laravel.md)
