PHP code example of dan-har / google-maps-services-php

1. Go to this page and download the library: Download dan-har/google-maps-services-php 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/ */

    

dan-har / google-maps-services-php example snippets


$placesService = new PlacesApi(new Client(), Auth::fromKey('GOOGLE_API_KEY'));

$placeId = 'ChIJN1t_tDeuEmsRUsoyG83frY4';

try {
    $placeResponse = $placesService->details($placeId);
} catch (ResourceRequestException $e) {
    // handle request exception, for example caused by timeout
}

// check if the response is ok, if so we have a result for the place id
if( ! $placeResponse->isOk()) {
    // handle request failure, for example no results, wrong place id etc.
}

$result = $placeResponse->result();

// check for address component
foreach($result->addressComponents as $addressComponent) {

    if($addressComponent->isType(AddressType::LOCALITY)) {
        //
    }
}

// get the geometry object.
$geometry = $result->geometry;

composer