PHP client library(SDK) for Google Maps API Web Services
$gmaps = new \yidas\googleMaps\Client(['key'=>'Your API Key']);
// Geocoding an address
$geocodeResult = $gmaps->geocode('Taipei 101, Taipei City, Taiwan 110');
// Look up an address with reverse geocoding
$reverseGeocodeResult = $gmaps->reverseGeocode([25.0339639, 121.5644722]);
// Request directions via public transit
$directionsResult = $gmaps->directions('National Palace Museum', 'Taipei 101', [
'mode' => "transit",
'departure_time' => time(),
]);
The PHP Client for Google Maps Services is a PHP Client library for the following Google Maps APIs:
- Directions API
- Distance Matrix API
- Elevation API
- Geocoding API
- Geolocation API
- Time Zone API
- Roads API (Required)
- Places API (Required)
- PHP 5.4.0+|7.0+ or higher
Each Google Maps Web Service request requires an API key or client ID. API keys are freely available with a Google Account at https://developers.google.com/console. The type of API key you need is a Server key.
To get an API key:
- Visit https://developers.google.com/console and log in with a Google Account.
- Select one of your existing projects, or create a new project.
- Enable the API(s) you want to use. The Client for Google Maps Services
accesses the following APIs:
- Directions API
- Distance Matrix API
- Elevation API
- Geocoding API
- Geolocation API
- Places API
- Roads API
- Time Zone API
- Create a new Server key.
- If you'd like to restrict requests to a specific IP address, do so now.
For guided help, follow the instructions for the Directions API. You only need one API key, but remember to enable all the APIs you need. For even more information, see the guide to [API keys][apikey].
Important: This key should be kept secret on your server.
Run Composer in your project:
composer require yidas/google-maps-services
Then you could call it after Composer is loaded depended on your PHP framework:
require __DIR__ . '/vendor/autoload.php';
use yidas\googleMaps\Client;
Before using any Google Maps Services, first you need to create a Client with configuration, then use the client to access Google Maps Services.
Create a Client using API key:
$gmaps = new \yidas\googleMaps\Client(['key'=>'Your API Key']);
If you use Google Maps APIs Premium Plan license instead of an API key, you could create Client using client ID and client secret (digital signature) for authentication.
$gmaps = new \yidas\googleMaps\Client([
'clientID' => 'Your client ID',
'clientSecret' => 'Your digital signature'
]);
You could set language for Client for all services:
$gmaps = new \yidas\googleMaps\Client(['key'=>'Your API Key', 'language'=>'zh-TW']);
Changing language during execution:
$gmaps->setLanguage('zh-TW');
// ...
// Request directions via public transit
$directionsResult = $gmaps->directions('National Palace Museum', 'Taipei 101', [
'mode' => "transit",
'departure_time' => time(),
]);
// Get the distance matrix data between two places
$distanceMatrixResult = $gmaps->distanceMatrix('National Palace Museum', 'Taipei 101');
// With Imperial units
$distanceMatrixResult = $gmaps->distanceMatrix('National Palace Museum', 'Taipei 101', [
'units' => 'imperial',
]);
// Get elevation by locations parameter
$elevationResult = $gmaps->elevation([25.0339639, 121.5644722]);
$elevationResult = $gmaps->elevation('25.0339639, 121.5644722');
// Geocoding an address
$geocodeResult = $gmaps->geocode('Taipei 101, Taipei City, Taiwan 110');
// Look up an address with reverse geocoding
$reverseGeocodeResult = $gmaps->reverseGeocode([25.0339639, 121.5644722]);
// Simple geolocate
$geolocateResult = $gmaps->geolocate([]);
// requests the time zone data for giving location
$timezoneResult = $gmaps->timezone([25.0339639, 121.5644722]);