PHP code example of alexpechkarev / google-maps

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

    

alexpechkarev / google-maps example snippets


composer 

'providers' => [
    ...
    GoogleMaps\ServiceProvider\GoogleMapsServiceProvider::class,
]

'aliases' => [
    ...
    'GoogleMaps' => GoogleMaps\Facade\GoogleMapsFacade::class,
]

php artisan vendor:publish --tag=googlemaps

/*
|----------------------------------
| Service Keys
|------------------------------------
*/

'key'       => 'ADD YOUR SERVICE KEY HERE',

$response = \GoogleMaps::load('geocoding')
		->setParam (['address' =>'santa cruz'])
 		->get();

$routeParams = [
    'origin' => [ /* ... origin details ... */ ],
    'destination' => [ /* ... destination details ... */ ],
    'travelMode' => 'DRIVE',
    // ... other Routes API parameters ...
];

$responseArray = \GoogleMaps::load('routes') // Use 'routes' service
    ->setParam($routeParams)
    ->setFieldMask('routes.duration,routes.distanceMeters,routes.polyline.encodedPolyline') // optional - used to specify fields to return 
    ->fetch(); // Use fetch() for Routes API

// $responseArray is already a PHP array
if (!empty($responseArray['routes'])) {
    // Process the route data
} else {
    // Handle errors or no routes found
}

$matrixParams = [
    'origins' => [ /* ... array of origins ... */ ],
    'destinations' => [ /* ... array of destinations ... */ ],
    'travelMode' => 'DRIVE',
    // ... other Route Matrix parameters ...
];

$responseArray = \GoogleMaps::load('routematrix') // Use 'routematrix' service
    ->setParam($matrixParams)
    ->setFieldMask('originIndex,destinationIndex,duration,distanceMeters,status,condition') // optional - used to specify fields to return 
    ->fetch(); // Use fetch() for Routes API

// $responseArray is already a PHP array
// Process the matrix data

$response = \GoogleMaps::load('geocoding')
		->setParam ([
		    'address'    =>'santa cruz',
            'components' => [
                    'administrative_area'  => 'TX',
                    'country'              => 'US',
                    ]
                ])
                ->get();

$endpoint = \GoogleMaps::load('geocoding')
   ->setParamByKey('address', 'santa cruz')
   ->setParamByKey('components.administrative_area', 'TX') //return $this
    ...

\GoogleMaps::load('geocoding')
...

$response = \GoogleMaps::load('geocoding')
		->setEndpoint('json')  // return $this
		...

$endpoint = \GoogleMaps::load('geocoding')
		->setEndpoint('json')
		->getEndpoint();

echo $endpoint; // output 'json'

$endpoint = \GoogleMaps::load('geocoding')
   ->setParamByKey('address', 'santa cruz')
   ->setParamByKey('components.administrative_area', 'TX') //return $this
    ...

$response = \GoogleMaps::load('geocoding')
                ->setParam([
                   'address'     => 'santa cruz',
                   'components'  => [
                        'administrative_area'   => 'TX',
                        'country'               => 'US',
                         ]
                     ]) // return $this
...

$response = \GoogleMaps::load('geocoding')
                ->setParamByKey('address', 'santa cruz')
                ->setParamByKey('components.administrative_area', 'TX')
                 ->get();

var_dump( json_decode( $response ) );  // output

/*
{\n
   "results" : [\n
      {\n
         "address_components" : [\n
            {\n
               "long_name" : "277",\n
               "short_name" : "277",\n
               "types" : [ "street_number" ]\n
            },\n
            ...
*/

$response = \GoogleMaps::load('geocoding')
                ->setParamByKey('latlng', '40.714224,-73.961452')
                 ->get('results.formatted_address');

var_dump( json_decode( $response ) );  // output

/*
array:1 [▼
  "results" => array:9 [▼
    0 => array:1 [▼
      "formatted_address" => "277 Bedford Ave, Brooklyn, NY 11211, USA"
    ]
    1 => array:1 [▼
      "formatted_address" => "Grand St/Bedford Av, Brooklyn, NY 11211, USA"
    ]
            ...
*/

$response = \GoogleMaps::load('routes')
                ->setParam($reqRoute) // <-- array see config file for all available parameters or Request Body
                ->fetch();

$response = \GoogleMaps::load('routes')
            ->setParam([
                        'origin' => [
                            'location' => [
                                'latLng' => [
                                    'latitude' => 37.419734,
                                    'longitude' => -122.0827784,
                                ],
                            ],
                        ],
                        'destination' => [
                            'location' => [
                                'latLng' => [
                                    'latitude' => 37.417670,
                                    'longitude' => -122.079595,
                                ],
                            ],
                        ],
                        'travelMode' => 'DRIVE',
                        'routingPreference' => 'TRAFFIC_AWARE',
                        'computeAlternativeRoutes' => false,
                        'routeModifiers' => [
                            'avoidTolls' => false,
                            'avoidHighways' => false,
                            'avoidFerries' => false,
                        ],
                        'languageCode' => 'en-US',
                        'units' => 'IMPERIAL',
                    ])
           ->isLocationOnEdge(37.41665,-122.08175);

    dd( $response  );  // true

$response = \GoogleMaps::load('routes')
            ->setParam([
                        'origin' => [
                            'location' => [
                                'latLng' => [
                                    'latitude' => 37.419734,
                                    'longitude' => -122.0827784,
                                ],
                            ],
                        ],
                        'destination' => [
                            'location' => [
                                'latLng' => [
                                    'latitude' => 37.417670,
                                    'longitude' => -122.079595,
                                ],
                            ],
                        ],
                        'travelMode' => 'DRIVE',
                        'routingPreference' => 'TRAFFIC_AWARE',
                        'computeAlternativeRoutes' => false,
                        'routeModifiers' => [
                            'avoidTolls' => false,
                            'avoidHighways' => false,
                            'avoidFerries' => false,
                        ],
                        'languageCode' => 'en-US',
                        'units' => 'IMPERIAL',
                    ])
           ->containsLocation(37.41764,-122.08293);

    dd( $response  );  // true