PHP code example of alex-kalanis / google-maps-php-services

1. Go to this page and download the library: Download alex-kalanis/google-maps-php-services library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

alex-kalanis / google-maps-php-services example snippets


// somewhere in configuration something like this

/// ... with anonymous function as setter in DI
function (): \kalanis\google_maps\ClientConfig
{
    return \kalanis\google_maps\ClientConfig::init('Your API Key');
}

class YourPresenter extends YourFramework
{
    public function __construct(
        // ... other used classes
        protected kalanis\google_maps\Client $mapService,
    ) {
    }

    public function process(): void
    {
        // Geocoding an address
        $geocodeResult = $this->mapService->geocode('Pelješki most, Croatia');

        // Look up an address with reverse geocoding
        $reverseGeocodeResult = $this->mapService->reverseGeocode(42.916667, 17.533333);

        // Request directions via public transit
        $directionsResult = $this->mapService->directions('Ploče', 'Dubrovnik', [
            'mode' => "transit",
            'departure_time' => time(),
        ]);
    }
}

/// app\Providers\AppServiceProvider.php
public function register()
{
    // ... other binds
    $this->app()->bind(\kalanis\google_maps\Client::class, function(
            Psr\Http\Client\ClientInterface $client,
            Psr\Http\Message\RequestInterface $request
        ) {
            return new \kalanis\google_maps\Client(
                $request,
                $client,
                \kalanis\google_maps\ClientConfig::init('Your API Key'),
            );
        }
    );
}


namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

#[Route('/your-maps')]
class YourMapsPresenter extends AbstractController
{
    public function __construct(
        // ... other used classes
        protected kalanis\google_maps\Client $mapService,
    ) {
    }

    #[Route('/geocode', name: 'maps_geocode', defaults: ['where' => 'Pelješki most, Croatia'], methods: ['GET'])]
    public function geocode(Request $request, string $where): Response
    {
        $geocodeResult = $this->mapService->geocode($where);
        return new JsonResponse($geocodeResult)
    }

    #[Route('/reverse-geocode', lat: '42.916667', lon: '17.533333', defaults: ['lat' => '42.916667', 'lon' => '17.533333'], methods: ['GET'])]
    public function reverseGeocode(Request $request, string $lat, string $lon): Response
    {
        $geocodeResult = $this->mapService->reverseGeocode([$lat, $lon]);
        return new JsonResponse($geocodeResult)
    }
}



use kalanis\google_maps\Client;

$gmaps = new \kalanis\google_maps\Client(
    new \PsrMock\Psr7\Request(),
    new \PsrMock\Psr18\Client(),
    new \kalanis\google_maps\ConfigClient('Your API Key'),
);

$gmaps = new \kalanis\google_maps\Client(
    new \PsrMock\Psr7\Request(),
    new \PsrMock\Psr18\Client(),
    new \kalanis\google_maps\ConfigClient('Your API Key', 'pt-br'),
);

// Get elevation by locations parameter
$elevationResult = $gmaps->elevation(25.0339639, 121.5644722);
$elevationResult = $gmaps->elevation('25.0339639', '121.5644722');

$routes = $gmaps->computeRoutes($originArray, $destinationArray, $fullBodyArray, $fieldMask)

// Get the route data between two places simply
$routes = $gmaps->computeRoutes([
        "location" => [
           "latLng" => [
              "latitude" => 37.419734,
              "longitude" => -122.0827784
           ]
        ]
    ],
    [
        "location" => [
           "latLng" => [
              "latitude" => 37.41767,
              "longitude" => -122.079595
           ]
        ]
    ]);

// Get the full route data between two places with full request data
$routes = $gmaps->computeRoutes([...], [...], ["travelMode": "DRIVE", ...], '*');

$roads = $gmaps->snapToRoads([[-35.27801,149.12958], [-35.28032,149.12907], [-35.28099,149.12929]]);

// Request directions via public transit
$directionsResult = $gmaps->directions('Milano', 'Venezia', [
    'mode' => "transit",
    'departure_time' => time(),
]);

// Get the distance matrix data between two places
$distanceMatrixResult = $gmaps->distanceMatrix('Canberra', 'Perth');

// With Imperial units
$distanceMatrixResult = $gmaps->distanceMatrix('Stonehenge', 'Bristol', [
    'units' => 'imperial',
]);

// Geocoding an address
$geocodeResult = $gmaps->geocode('Avenida Maracanã 350, Rio de Janeiro');

// Look up an address with reverse geocoding
$reverseGeocodeResult = $gmaps->reverseGeocode(-22.912167, -43.230164);

// Simple geolocate
$geolocateResult = $gmaps->geolocate([]);

// requests the time zone data for given location
$timezoneResult = $gmaps->timezone([25.381111, 83.021389]); // Sárnáth

// requests the nearby points for given location
$nearbyResult = $gmaps->nearby('restaurant', [25.71874, 32.6574]); // in Luxor

// requests the place points for given location by given place
$nearbyResult = $gmaps->findPlace('Champs Elysees', 'restaurant', ['name', 'current_opening_hours']);

// requests the place points for given location by given text
$nearbyResult = $gmaps->findText('Sagrada Familia', 350, [], 3, 0, true);

// requests the details about place point
$nearbyResult = $gmaps->placeDetails('ChIJN1t_tDeuEmsRUsoyG83frY4', ['name', 'current_opening_hours']);