How to use the Google Maps package (General Pattern)
masterThe package follows a consistent pattern for making requests:
- Load a service: Use
\GoogleMaps::load('service-name'). - Set parameters: Use
setParam([...])for arrays orsetParamByKey('key', 'value')for single values. For nested arrays, use dot notation (e.g.,components.country). - Execute the request:
- Use
->get()for most APIs (Geocoding, Elevation, etc.). Note thatget()returns a JSON string that you must decode into a PHP variable. - Use
->fetch()ONLY for the Routes API (routesandroutematrixservices).fetch()returns a PHP array directly.
- Use
// Standard API pattern
$response = \GoogleMaps::load('geocoding')
->setParam(['address' => 'santa cruz'])
->get();
// Routes API pattern
$response = \GoogleMaps::load('routes')
->setParam($params)
->fetch();