├── _config.yml ├── .gitignore ├── composer.json ├── src ├── Geolocation.php ├── Directions.php ├── DistanceMatrix.php ├── Elevation.php ├── Roads.php ├── Timezone.php ├── Routes.php ├── Geocoding.php ├── Service.php └── Client.php ├── .github └── workflows │ └── php.yml ├── LICENSE └── README.md /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-minimal -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /composer.lock 2 | /vendor 3 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "yidas/google-maps-services", 3 | "description": "PHP client library(SDK) for Google Maps API Web Services", 4 | "keywords": ["Google maps", "services", "php", "sdk"], 5 | "homepage": "https://github.com/yidas/google-maps-services-php", 6 | "type": "library", 7 | "license": "MIT", 8 | "support": { 9 | "issues": "https://github.com/yidas/google-maps-services-php/issues", 10 | "source": "https://github.com/yidas/google-maps-services-php" 11 | }, 12 | "require": { 13 | "php": ">=5.4", 14 | "guzzlehttp/guzzle": "~6.5.6|^7.4.3" 15 | }, 16 | "autoload": { 17 | "psr-4": { 18 | "yidas\\googleMaps\\": "src/" 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/Geolocation.php: -------------------------------------------------------------------------------- 1 | 12 | * @since 1.0.0 13 | * @see https://developers.google.com/maps/documentation/geolocation/ 14 | */ 15 | class Geolocation extends Service 16 | { 17 | /** 18 | * Replace all 19 | */ 20 | const API_URL = 'https://www.googleapis.com/geolocation/v1/geolocate'; 21 | 22 | /** 23 | * Geolocate 24 | * 25 | * @param Client $client 26 | * @param array Body parameters 27 | * @return array Result 28 | */ 29 | public static function geolocate(Client $client, $bodyParams=[]) 30 | { 31 | return self::requestHandler($client, self::API_URL, [], 'POST', $bodyParams); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Directions.php: -------------------------------------------------------------------------------- 1 | 12 | * @since 1.0.0 13 | * @see https://developers.google.com/maps/documentation/directions/ 14 | */ 15 | class Directions extends Service 16 | { 17 | const API_URL = 'https://maps.googleapis.com/maps/api/directions/json'; 18 | 19 | /** 20 | * Directions 21 | * 22 | * @param Client $client 23 | * @param string $origin 24 | * @param string $destination 25 | * @param array Query parameters 26 | * @return array Result 27 | */ 28 | public static function directions(Client $client, $origin, $destination, $params=[]) 29 | { 30 | $params['origin'] = (string) $origin; 31 | $params['destination'] = (string) $destination; 32 | 33 | return self::requestHandler($client, self::API_URL, $params); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /.github/workflows/php.yml: -------------------------------------------------------------------------------- 1 | name: PHP Composer 2 | 3 | on: 4 | push: 5 | branches: [ "master" ] 6 | pull_request: 7 | branches: [ "master" ] 8 | 9 | permissions: 10 | contents: read 11 | 12 | jobs: 13 | build: 14 | 15 | runs-on: ubuntu-latest 16 | 17 | steps: 18 | - uses: actions/checkout@v3 19 | 20 | - name: Validate composer.json and composer.lock 21 | run: composer validate --strict 22 | 23 | - name: Cache Composer packages 24 | id: composer-cache 25 | uses: actions/cache@v3 26 | with: 27 | path: vendor 28 | key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }} 29 | restore-keys: | 30 | ${{ runner.os }}-php- 31 | 32 | - name: Install dependencies 33 | run: composer install --prefer-dist --no-progress 34 | 35 | # Add a test script to composer.json, for instance: "test": "vendor/bin/phpunit" 36 | # Docs: https://getcomposer.org/doc/articles/scripts.md 37 | 38 | # - name: Run test suite 39 | # run: composer run-script test 40 | -------------------------------------------------------------------------------- /src/DistanceMatrix.php: -------------------------------------------------------------------------------- 1 | 12 | * @since 1.0.0 13 | * @see https://developers.google.com/maps/documentation/distance-matrix/ 14 | */ 15 | class DistanceMatrix extends Service 16 | { 17 | const API_URL = 'https://maps.googleapis.com/maps/api/distancematrix/json'; 18 | 19 | /** 20 | * Distance matrix 21 | * 22 | * @param Client $client 23 | * @param string $origin 24 | * @param string $destination 25 | * @param array Query parameters 26 | * @return array Result 27 | */ 28 | public static function distanceMatrix(Client $client, $origins, $destinations, $params=[]) 29 | { 30 | $params['origins'] = (string) $origins; 31 | $params['destinations'] = (string) $destinations; 32 | 33 | return self::requestHandler($client, self::API_URL, $params); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Nick Tsai 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/Elevation.php: -------------------------------------------------------------------------------- 1 | 12 | * @since 1.0.0 13 | * @see https://developers.google.com/maps/documentation/elevation/ 14 | */ 15 | class Elevation extends Service 16 | { 17 | const API_URL = 'https://maps.googleapis.com/maps/api/elevation/json'; 18 | 19 | /** 20 | * Elevation 21 | * 22 | * @param Client $client 23 | * @param string $locations 24 | * @param array Query parameters 25 | * @return array Result 26 | */ 27 | public static function elevation(Client $client, $locations, $params=[]) 28 | { 29 | // `locations` seems to only allow `lat,lng` pattern 30 | if (is_string($locations)) { 31 | 32 | $params['locations'] = $locations; 33 | 34 | } else { 35 | 36 | list($lat, $lng) = $locations; 37 | $params['locations'] = "{$lat},{$lng}"; 38 | } 39 | 40 | return self::requestHandler($client, self::API_URL, $params); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/Roads.php: -------------------------------------------------------------------------------- 1 | 12 | * @since 1.0.0 13 | * @see https://developers.google.com/maps/documentation/roads 14 | */ 15 | class Roads extends Service 16 | { 17 | const API_URL = 'https://roads.googleapis.com/v1/snapToRoads'; 18 | 19 | const LANGUAGE_METHOD = false; 20 | 21 | /** 22 | * Reverse Geocode 23 | * 24 | * @param Client $client 25 | * @param string $address 26 | * @param array Query parameters 27 | * @return array Result 28 | */ 29 | public static function snapToRoads(Client $client, $path=null, $params=[]) 30 | { 31 | if (is_array($path)) { 32 | $pathString = ''; 33 | foreach ($path as $key => $eachPathArray) { 34 | $pathString = ($key != 0) ? $pathString . '|' : $pathString; 35 | $pathString .= implode(',', $eachPathArray); 36 | } 37 | $params['path'] = $pathString; 38 | } 39 | 40 | $params['interpolate'] = $params['interpolate'] ?? 'true'; 41 | 42 | return self::requestHandler($client, self::API_URL, $params); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/Timezone.php: -------------------------------------------------------------------------------- 1 | 12 | * @since 1.0.0 13 | * @see https://developers.google.com/maps/documentation/timezone/ 14 | */ 15 | class Timezone extends Service 16 | { 17 | const API_URL = 'https://maps.googleapis.com/maps/api/timezone/json'; 18 | 19 | /** 20 | * Timezone 21 | * 22 | * @param Client $client 23 | * @param string $location 24 | * @param string $timestamp 25 | * @param array Query parameters 26 | * @return array Result 27 | */ 28 | public static function timezone(Client $client, $location, $timestamp=null, $params=[]) 29 | { 30 | // `location` seems to only allow `lat,lng` pattern 31 | if (is_string($location)) { 32 | 33 | $params['location'] = $location; 34 | 35 | } else { 36 | 37 | list($lat, $lng) = $location; 38 | $params['location'] = "{$lat},{$lng}"; 39 | } 40 | 41 | // Timestamp 42 | $params['timestamp'] = ($timestamp) ?: time(); 43 | 44 | return self::requestHandler($client, self::API_URL, $params); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/Routes.php: -------------------------------------------------------------------------------- 1 | 12 | * @since 1.2.0 13 | * @see https://developers.google.com/maps/documentation/routes/ 14 | */ 15 | class Routes extends Service 16 | { 17 | const API_URL = 'https://routes.googleapis.com/directions/v2'; 18 | 19 | const LANGUAGE_METHOD = 'body'; 20 | 21 | /** 22 | * Distance matrix 23 | * 24 | * @param Client $client 25 | * @param array $origin 26 | * @param array $destination 27 | * @param array $body Full body 28 | * @param array $headers 29 | * @return array Result 30 | */ 31 | public static function computeRoutes(Client $client, $origin, $destination, $body=[], $fieldMask=[]) 32 | { 33 | $fullApiUrl = self::API_URL . ':computeRoutes'; 34 | 35 | $requestBody = $body; 36 | $requestBody['origin'] = $origin ?? $requestBody['origin'] ?? []; 37 | $requestBody['destination'] = $destination ?? $requestBody['destination'] ?? []; 38 | 39 | // Header 40 | $fieldMask = $fieldMask ? $fieldMask : ['routes.duration', 'routes.distanceMeters', 'routes.polyline.encodedPolyline']; 41 | $fieldMask = is_array($fieldMask) ? implode(",", $fieldMask) : $fieldMask; 42 | $headers = [ 43 | 'X-Goog-Api-Key' => $client->getApiKey(), 44 | 'X-Goog-FieldMask' => $fieldMask, 45 | ]; 46 | 47 | return self::requestHandler($client, $fullApiUrl, [], 'POST', $requestBody, $headers); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/Geocoding.php: -------------------------------------------------------------------------------- 1 | 12 | * @since 1.0.0 13 | * @see https://developers.google.com/maps/documentation/geocoding/ 14 | */ 15 | class Geocoding extends Service 16 | { 17 | const API_URL = 'https://maps.googleapis.com/maps/api/geocode/json'; 18 | 19 | /** 20 | * Reverse Geocode 21 | * 22 | * @param Client $client 23 | * @param string $address 24 | * @param array Query parameters 25 | * @return array Result 26 | */ 27 | public static function geocode(Client $client, $address=null, $params=[]) 28 | { 29 | if (is_string($address)) 30 | $params['address'] = $address; 31 | 32 | return self::requestHandler($client, self::API_URL, $params); 33 | } 34 | 35 | /** 36 | * Reverse Geocode 37 | * 38 | * @param Client $client 39 | * @param array|string $latlng ['lat', 'lng'] or place_id string 40 | * @param array Query parameters 41 | * @return array Result 42 | */ 43 | public static function reverseGeocode(Client $client, $latlng, $params=[]) 44 | { 45 | // Check if latlng param is a place_id string. 46 | // place_id strings do not contain commas; latlng strings do. 47 | if (is_string($latlng)) { 48 | 49 | $params['place_id'] = $latlng; 50 | 51 | } else { 52 | 53 | list($lat, $lng) = $latlng; 54 | $params['latlng'] = "{$lat},{$lng}"; 55 | } 56 | 57 | return self::requestHandler($client, self::API_URL, $params); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/Service.php: -------------------------------------------------------------------------------- 1 | 9 | * @since 1.2.0 10 | */ 11 | abstract class Service 12 | { 13 | /** 14 | * Define by each service 15 | * 16 | * @param string 17 | */ 18 | const API_URL = ''; 19 | 20 | /** 21 | * Language setting method 22 | * 23 | * 'query' for query string method, 'body' for request body 24 | * 25 | * @param string 26 | */ 27 | const LANGUAGE_METHOD = 'query'; 28 | 29 | /** 30 | * Request Handler 31 | * 32 | * @param Client $client 33 | * @param string $apiUrl 34 | * @param array $params 35 | * @param string $method HTTP request method 36 | * @param array $body 37 | * @param array $headers 38 | * @return array|mixed Formated result 39 | */ 40 | protected static function requestHandler(Client $client, $apiUrl, $params, $method='GET', $body=[], $headers=[]) 41 | { 42 | // Language setting for query string 43 | if (static::LANGUAGE_METHOD && $client->getLanguage()) { 44 | 45 | switch (static::LANGUAGE_METHOD) { 46 | case 'body': 47 | if (!isset($body['languageCode'])) { 48 | $body['languageCode'] = $client->getLanguage(); 49 | } 50 | break; 51 | 52 | case 'query': 53 | default: 54 | $params['language'] = $client->getLanguage(); 55 | break; 56 | } 57 | } 58 | 59 | // Body 60 | $bodyString = ($body) ? json_encode($body, JSON_UNESCAPED_SLASHES) : null; 61 | 62 | // Header 63 | $defaultHeaders = [ 64 | 'Content-Type' => 'application/json', 65 | ]; 66 | $headers = array_merge($defaultHeaders, $headers); 67 | 68 | $response = $client->request($apiUrl, $params, $method, $bodyString, $headers); 69 | $result = $response->getBody()->getContents(); 70 | $result = json_decode($result, true); 71 | 72 | // Error Handler 73 | if (200 != $response->getStatusCode()) 74 | return $result; 75 | // Error message Checker (200 situation form Google Maps API) 76 | elseif (isset($result['error_message'])) 77 | return $result; 78 | 79 | // `results` parsing from Google Maps API, while pass parsing on error 80 | return isset($result['results']) ? $result['results'] : $result; 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/Client.php: -------------------------------------------------------------------------------- 1 | 12 | * @version 1.2.0 13 | * 14 | * @method array directions(string $origin, string $destination, array $params=[]) 15 | * @method array distanceMatrix(string $origin, string $destination, array $params=[]) 16 | * @method array elevation(string $locations, array $params=[]) 17 | * @method array geocode(string $address, array $params=[]) 18 | * @method array reverseGeocode(string $latlng, array $params=[]) 19 | * @method array computeRoutes(array $origin, array $destination, array $body=[], array $fieldMask=[]) 20 | * @method array geolocate(array $bodyParams=[]) 21 | * @method array timezone(string $location, string $timestamp=null, array $params=[]) 22 | */ 23 | class Client 24 | { 25 | /** 26 | * Google Maps Platform base API host 27 | */ 28 | // const API_HOST = 'https://maps.googleapis.com'; 29 | 30 | /** 31 | * For service autoload 32 | * 33 | * @see http://php.net/manual/en/language.namespaces.rules.php 34 | */ 35 | const SERVICE_NAMESPACE = "\\yidas\\googleMaps\\"; 36 | 37 | /** 38 | * For Client-Service API method director 39 | * 40 | * @var array Method => Service Class name 41 | */ 42 | protected static $serviceMethodMap = [ 43 | 'directions' => 'Directions', 44 | 'distanceMatrix' => 'DistanceMatrix', 45 | 'elevation' => 'Elevation', 46 | 'geocode' => 'Geocoding', 47 | 'reverseGeocode' => 'Geocoding', 48 | 'geolocate' => 'Geolocation', 49 | 'timezone' => 'Timezone', 50 | 'computeRoutes' => 'Routes', 51 | 'snapToRoads' => 'Roads', 52 | ]; 53 | 54 | /** 55 | * Google API Key 56 | * 57 | * Authenticating by API Key, otherwise by client ID/digital signature 58 | * 59 | * @var string 60 | */ 61 | protected $apiKey; 62 | 63 | /** 64 | * Google client ID 65 | * 66 | * @var string 67 | */ 68 | protected $clientID; 69 | 70 | /** 71 | * Google client's digital signature 72 | * 73 | * @var string 74 | */ 75 | protected $clientSecret; 76 | 77 | /** 78 | * @var \GuzzleHttp\Client 79 | */ 80 | protected $httpClient; 81 | 82 | /** 83 | * Google Maps default language 84 | * 85 | * @var string ex. 'zh-TW' 86 | */ 87 | protected $language; 88 | 89 | /** 90 | * Constructor 91 | * 92 | * @param string|array $optParams API Key or option parameters 93 | * 'key' => Google API Key 94 | * 'clientID' => Google clientID 95 | * 'clientSecret' => Google clientSecret 96 | * @return self 97 | */ 98 | function __construct($optParams) 99 | { 100 | // Quick setting for API Key 101 | if (is_string($optParams)) { 102 | // Params as a string key 103 | $key = $optParams; 104 | $optParams = []; 105 | $optParams['key'] = $key; 106 | } 107 | 108 | // Assignment 109 | $key = isset($optParams['key']) ? $optParams['key'] : null; 110 | $clientID = isset($optParams['clientID']) ? $optParams['clientID'] : null; 111 | $clientSecret = isset($optParams['clientSecret']) ? $optParams['clientSecret'] : null; 112 | $defaultLang = isset($optParams['language']) ? $optParams['language'] : null; 113 | 114 | // Use API Key 115 | if ($key) { 116 | 117 | if ($clientID || $clientSecret) { 118 | throw new Exception("clientID/clientSecret should not set when using key", 400); 119 | } 120 | 121 | $this->apiKey = (string) $key; 122 | } 123 | // Use clientID/clientSecret 124 | elseif ($clientID && $clientSecret) { 125 | 126 | $this->clientID = (string) $clientID; 127 | $this->clientSecret = (string) $clientSecret; 128 | } 129 | else { 130 | 131 | throw new Exception("Unable to set Client credential due to your wrong params", 400); 132 | } 133 | 134 | // Default Language setting 135 | if ($defaultLang) { 136 | $this->setLanguage($defaultLang); 137 | } 138 | 139 | // Load GuzzleHttp\Client 140 | $this->httpClient = new HttpClient([ 141 | // 'base_uri' => self::API_HOST, 142 | 'timeout' => 5.0, 143 | ]); 144 | 145 | return $this; 146 | } 147 | 148 | /** 149 | * Request Google Map API 150 | * 151 | * @param string $url 152 | * @param array $params 153 | * @param string $method HTTP request method 154 | * @param string $body 155 | * @param array $headers 156 | * @return GuzzleHttp\Psr7\Response 157 | */ 158 | public function request($url, $params=[], $method='GET', $body=null, $headers=null) 159 | { 160 | // Guzzle request options 161 | $options = [ 162 | 'http_errors' => false, 163 | ]; 164 | 165 | // Parameters for Auth 166 | $defaultParams = ($this->apiKey) 167 | ? ['key' => $this->apiKey] 168 | : ['client' => $this->clientID, 'signature' => $this->clientSecret]; 169 | 170 | // Query String 171 | $options['query'] = array_merge($defaultParams, $params); 172 | 173 | // Body 174 | if ($body) { 175 | $options['body'] = $body; 176 | } 177 | 178 | // Headers 179 | if ($headers) { 180 | $options['headers'] = $headers; 181 | } 182 | 183 | // $options['debug'] = true; 184 | return $this->httpClient->request($method, $url, $options); 185 | } 186 | 187 | /** 188 | * Set default language for Google Maps API 189 | * 190 | * @param string $language ex. 'zh-TW' 191 | * @return self 192 | */ 193 | public function setLanguage($language=null) 194 | { 195 | $this->language = $language; 196 | 197 | return $this; 198 | } 199 | 200 | /** 201 | * Get current language setting for Google Maps API 202 | * 203 | * @return string 204 | */ 205 | public function getLanguage() 206 | { 207 | return $this->language; 208 | } 209 | 210 | /** 211 | * Get current API key in client 212 | * 213 | * @return string 214 | */ 215 | public function getApiKey() 216 | { 217 | return $this->apiKey; 218 | } 219 | 220 | /** 221 | * Client methods refer to each service 222 | * 223 | * All service methods from Client calling would leave out the first argument (Client itself). 224 | * 225 | * @param string Client's method name 226 | * @param array Method arguments 227 | * @return mixed Current service method return 228 | * @example 229 | * $equal = \yidas\googleMaps\Geocoding::geocode($client, 'Address'); 230 | * $equal = $client->geocode('Address'); 231 | */ 232 | public function __call($method, $arguments) 233 | { 234 | // Matching self::$serviceMethodMap is required 235 | if (!isset(self::$serviceMethodMap[$method])) 236 | throw new Exception("Call to undefined method ".__CLASS__."::{$method}()", 400); 237 | 238 | // Get the service mapped by method 239 | $service = self::$serviceMethodMap[$method]; 240 | 241 | // Fill Client in front of arguments 242 | array_unshift($arguments, $this); 243 | 244 | return call_user_func_array([self::SERVICE_NAMESPACE . $service, $method], $arguments); 245 | } 246 | } 247 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | 3 | 4 | 5 |

Google Maps Services for PHP

6 |
7 |

8 | 9 | PHP client library(SDK) for Google Maps API Web Services 10 | 11 | [![Latest Stable Version](https://poser.pugx.org/yidas/google-maps-services/v/stable?format=flat-square)](https://packagist.org/packages/yidas/google-maps-services) 12 | [![License](https://poser.pugx.org/yidas/google-maps-services/license?format=flat-square)](https://packagist.org/packages/yidas/google-maps-services) 13 | [![Total Downloads](https://poser.pugx.org/yidas/google-maps-services/downloads?format=flat-square)](https://packagist.org/packages/yidas/google-maps-services) 14 | [![Monthly Downloads](https://poser.pugx.org/yidas/google-maps-services/d/monthly?format=flat-square)](https://packagist.org/packages/yidas/google-maps-services) 15 | 16 | 17 | OUTLINE 18 | ------- 19 | 20 | - [Demonstration](#demonstration) 21 | - [Description](#description) 22 | - [Requirements](#requirements) 23 | - [API keys](#api-keys) 24 | - [Installation](#installation) 25 | - [Usage](#usage) 26 | - [Client](#client) 27 | - [Google Maps APIs Premium Plan license](#google-maps-apis-premium-plan-license) 28 | - [Language](#language) 29 | - [APIs](#apis) 30 | 31 | --- 32 | 33 | DEMONSTRATION 34 | ------------- 35 | 36 | 37 | ```php 38 | $gmaps = new \yidas\googleMaps\Client(['key'=>'Your API Key']); 39 | 40 | // Geocoding an address 41 | $geocodeResult = $gmaps->geocode('Taipei 101, Taipei City, Taiwan 110'); 42 | 43 | // Look up an address with reverse geocoding 44 | $reverseGeocodeResult = $gmaps->reverseGeocode([25.0339639, 121.5644722]); 45 | 46 | // Request directions via public transit 47 | $directionsResult = $gmaps->directions('National Palace Museum', 'Taipei 101', [ 48 | 'mode' => "transit", 49 | 'departure_time' => time(), 50 | ]); 51 | ``` 52 | 53 | --- 54 | 55 | 56 | DESCRIPTION 57 | ----------- 58 | 59 | The PHP Client for Google Maps Services is a PHP Client library for the following [Google Maps APIs](https://developers.google.com/maps): 60 | 61 | - Maps 62 | - [Elevation API](#elevation-api) ([Google Doc](https://developers.google.com/maps/documentation/elevation/)) 63 | - Routes 64 | - [Routes API](#routes-api) ([Google Doc](https://developers.google.com/maps/documentation/routes)) 65 | - [Roads API](#roads-api) ([Google Doc](https://developers.google.com/maps/documentation/roads)) 66 | - [Directions API](#directions-api) ([Google Doc](https://developers.google.com/maps/documentation/directions/)) 67 | - [Distance Matrix API](#distance-matrix-api) ([Google Doc](https://developers.google.com/maps/documentation/distancematrix/)) 68 | - Places 69 | - [Places API] (TBD) 70 | - [Geocoding API](#geocoding-api) ([Google Doc](https://developers.google.com/maps/documentation/geocoding/)) 71 | - [Geolocation API](#geolocation-api) ([Google Doc](https://developers.google.com/maps/documentation/geolocation/)) 72 | - [Time Zone API](#time-zone-api) ([Google Doc](https://developers.google.com/maps/documentation/timezone/)) 73 | 74 | --- 75 | 76 | REQUIREMENTS 77 | ------------ 78 | 79 | - PHP 7.0+ or higher 80 | 81 | ### API keys 82 | 83 | Each Google Maps Web Service request requires an API key or client ID. API keys 84 | are freely available with a Google Account at 85 | https://developers.google.com/console. The type of API key you need is a 86 | **Server key**. 87 | 88 | To get an API key: 89 | 90 | 1. Visit https://developers.google.com/console and log in with 91 | a Google Account. 92 | 2. Select one of your existing projects, or create a new project. 93 | 3. Enable the Google Maps Services API(s) you plan to use, such as: 94 | * Directions API 95 | * Distance Matrix API 96 | * Geocoding API 97 | * ... 98 | 4. Create a new **Server key**. 99 | 5. If you'd like to restrict requests to a specific IP address, do so now. 100 | 101 | For guided help, follow the instructions for the [Directions API][directions-key]. You only need one API key, but remember to enable all the APIs you need. 102 | For even more information, see the guide to [API keys][apikey]. 103 | 104 | **Important:** This key should be kept secret on your server. 105 | 106 | --- 107 | 108 | INSTALLATION 109 | ------------ 110 | 111 | Run Composer in your project: 112 | 113 | composer require yidas/google-maps-services 114 | 115 | Then you could call it after Composer is loaded depended on your PHP framework: 116 | 117 | ```php 118 | require __DIR__ . '/vendor/autoload.php'; 119 | 120 | use yidas\googleMaps\Client; 121 | ``` 122 | 123 | --- 124 | 125 | USAGE 126 | ----- 127 | 128 | Before using any Google Maps Services, first you need to create a Client with configuration, then use the client to access Google Maps Services. 129 | 130 | ### Client 131 | 132 | Create a Client using [API key]((#api-keys)): 133 | 134 | ```php 135 | $gmaps = new \yidas\googleMaps\Client(['key'=>'Your API Key']); 136 | ``` 137 | 138 | #### Google Maps APIs Premium Plan license 139 | 140 | If you use [Google Maps APIs Premium Plan license](https://developers.google.com/maps/documentation/directions/get-api-key#client-id) instead of an API key, you could create Client using client ID and client secret (digital signature) for authentication. 141 | 142 | ```php 143 | $gmaps = new \yidas\googleMaps\Client([ 144 | 'clientID' => 'Your client ID', 145 | 'clientSecret' => 'Your digital signature' 146 | ]); 147 | ``` 148 | 149 | #### Language 150 | 151 | You could set language for Client for all services: 152 | 153 | ```php 154 | $gmaps = new \yidas\googleMaps\Client(['key'=>'Your API Key', 'language'=>'zh-TW']); 155 | ``` 156 | 157 | > [list of supported languages - Google Maps Platform](https://developers.google.com/maps/faq#languagesupport) 158 | 159 | Changing language during execution: 160 | 161 | ```php 162 | $gmaps->setLanguage('zh-TW'); 163 | // ... 164 | ``` 165 | 166 | ### APIs 167 | 168 | #### Elevation API 169 | 170 | [Elevation API overview | Google for Developers](https://developers.google.com/maps/documentation/elevation/overview) 171 | 172 | ```php 173 | // Get elevation by locations parameter 174 | $elevationResult = $gmaps->elevation([25.0339639, 121.5644722]); 175 | $elevationResult = $gmaps->elevation('25.0339639, 121.5644722'); 176 | ``` 177 | 178 | #### Routes API 179 | 180 | [Get a route | Google for Developers](https://developers.google.com/maps/documentation/routes/compute_route_directions) 181 | 182 | ```php 183 | $routes = $gmaps->computeRoutes($originArray, $destinationArray, $fullBodyArray, $fieldMask) 184 | 185 | // Get the route data between two places simply 186 | $routes = $gmaps->computeRoutes([ 187 | "location" => [ 188 | "latLng" => [ 189 | "latitude" => 37.419734, 190 | "longitude" => -122.0827784 191 | ] 192 | ] 193 | ], 194 | [ 195 | "location" => [ 196 | "latLng" => [ 197 | "latitude" => 37.41767, 198 | "longitude" => -122.079595 199 | ] 200 | ] 201 | ]); 202 | 203 | // Get the full route data between two places with full request data 204 | $routes = $gmaps->computeRoutes([...], [...], ["travelMode": "DRIVE", ...], '*'); 205 | ``` 206 | 207 | #### Roads API 208 | 209 | [Snap to Roads | Google for Developers](https://developers.google.com/maps/documentation/roads/snap) 210 | 211 | ```php 212 | $roads = $gmaps->snapToRoads([[-35.27801,149.12958], [-35.28032,149.12907], [-35.28099,149.12929]]); 213 | ``` 214 | 215 | #### Directions API 216 | 217 | [Getting directions | Google for Developers](https://developers.google.com/maps/documentation/directions/get-directions) 218 | 219 | ```php 220 | // Request directions via public transit 221 | $directionsResult = $gmaps->directions('National Palace Museum', 'Taipei 101', [ 222 | 'mode' => "transit", 223 | 'departure_time' => time(), 224 | ]); 225 | ``` 226 | 227 | 228 | #### Distance Matrix API 229 | 230 | [Get started with the Distance Matrix API | Google for Developers](https://developers.google.com/maps/documentation/distance-matrix/start) 231 | 232 | ```php 233 | // Get the distance matrix data between two places 234 | $distanceMatrixResult = $gmaps->distanceMatrix('National Palace Museum', 'Taipei 101'); 235 | 236 | // With Imperial units 237 | $distanceMatrixResult = $gmaps->distanceMatrix('National Palace Museum', 'Taipei 101', [ 238 | 'units' => 'imperial', 239 | ]); 240 | ``` 241 | 242 | #### Geocoding API 243 | 244 | [Geocoding API overview | Google for Developers](https://developers.google.com/maps/documentation/geocoding/overview) 245 | 246 | ```php 247 | // Geocoding an address 248 | $geocodeResult = $gmaps->geocode('Taipei 101, Taipei City, Taiwan 110'); 249 | 250 | // Look up an address with reverse geocoding 251 | $reverseGeocodeResult = $gmaps->reverseGeocode([25.0339639, 121.5644722]); 252 | ``` 253 | 254 | #### Geolocation API 255 | 256 | [Geolocation API overview | Google for Developers](https://developers.google.com/maps/documentation/geolocation/overview) 257 | 258 | ```php 259 | // Simple geolocate 260 | $geolocateResult = $gmaps->geolocate([]); 261 | ``` 262 | 263 | #### Time Zone API 264 | 265 | [Time Zone API overview | Google for Developers](https://developers.google.com/maps/documentation/timezone/overview) 266 | 267 | ```php 268 | // requests the time zone data for giving location 269 | $timezoneResult = $gmaps->timezone([25.0339639, 121.5644722]); 270 | ``` 271 | 272 | --------------------------------------------------------------------------------